Home > Java > JavaBase > How to run jar package in java?

How to run jar package in java?

Release: 2019-12-03 15:24:18
Original
9955 people have browsed it

How to run jar package in java?

How to run jar package in java: (Recommended: java video tutorial)

Everyone knows that a java application project can be packaged into a jar, of course you must specify a main class with a main function as the program entry point of your jar package.

The specific method is to modify the MANIFEST.MF file under the META-INF directory in the jar package.

For example, there is a jar package called test.jar, which contains a main class with a main function: test.someClassName

We only need to add the following sentence to MANIFEST.MF:

Main-Class: test.someClassName
Copy after login

Then we can enter java -jar test.jar in the console to run the jar.

Methods to run third-party jar packages:

Method 1. Use Bootstrap Classloader to load these classes

We can use the following parameters at runtime :

-Xbootclasspath: Completely replaces the system Java classpath. It is best not to use it.
-Xbootclasspath/a: Loaded after the system class is loaded. Generally use this.
-Xbootclasspath/p: Load before loading the system class, pay attention to its use, it will not be good if it conflicts with the system class.

win32 java -Xbootclasspath/a: some.jar;some2.jar; -jar test.jar
unix    java -Xbootclasspath/a: some.jar:some2.jar: -jar test.jar
Copy after login

Each jar is separated by a semicolon in the win32 system, and a colon is used in the unix system. Separate

Method 2. Use Extension Classloader to load

You can throw all the jars that need to be loaded into %JRE_HOME%/lib/ext, in this directory The jar package will be loaded by Extension Classloader after Bootstrap Classloader completes its work. Very convenient and very worry-free. :)

Method three, still use AppClassloader to load, but no classpath parameter is required

We add the following code in MANIFEST.MF:

Class-Path: lib/some.jar

lib is a subdirectory in the same directory as test.jar, and the some.jar package to be referenced by test.jar is in it.

If there are multiple jar packages that need to be referenced:

Class-Path: lib/some.jar lib/some2.jar

Use spaces for each individual jar Just separate. Be careful to use relative paths.

Method 4. Customize Classloader to load

This method is the ultimate solution. Basically, all well-known java applications do this, such as tomcat and jboss etc.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to run jar package in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template