Four Methods for running the jar application to reference other jar packages)

Source: Internet
Author: User

(1) operate in eclipse

The above version of eclipse is:

(2) The following describes the relevant principles and the operation details for manually completing this goal:

We all know that a Java application project can be packaged into a jar. Of course, you must specify a main class with the main function as the program entry for your jar package.

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

For example, a jar package named test. jar contains a main class: Test. someclassname with the main function.

Add the following sentence to manifest. MF:

Main-class: Test. someclassname

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

However, we need to reference other third-party jar packages in this project. In eclipse, We reference this as a project jar package. jar package, which was put under the Lib subdirectory of the project at that time, and some. jar, but Java-jar is used to execute the test. when the JAR file is used, the system reports that the class cannot be found because the JAR file cannot be referenced in its own JAR file.

What should we do?

Can I add it to classpath at runtime? Add the classpath parameter while running the JAR file:

Java-classpath some. Jar-Jar test. Jar

This method is not feasible because the jar specified by classpath is loaded by appclassloader. After the-jar parameter is added to the Java command, appclassloader only pays attention to test. the classpath parameter is invalid because the class is in the jar range.

How can I reference other jar packages?

Method 1: use Bootstrap classloader to load these classes.

We can use the following parameters at runtime:

-Xbootclasspath: Completely replaces the system's Java classpath. It is best not to use it.

-Xbootclasspath/A: loaded after the system class is loaded. This is generally used. -Xbootclasspath/P: It is loaded before the system class is loaded. Pay attention to the usage. conflicts with the system class are not good.

Win32 Java-xbootclasspath/A: Some. jar; some2.jar;-jar test. jarunix Java-xbootclasspath/A: Some. jar: some2.jar:-jar test. in jarwin32 systems, each jar is separated by a semicolon, and in Unix systems, it is separated by a colon.

Method 2: Use extension classloader to load

You can throw all the jar files to be loaded to % jre_home %/lib/EXT. The jar package in this directory will be loaded by the extension classloader after the bootstrap classloader is finished. Very convenient and worry-free. :)

Method 3: Use appclassloader for loading, but the classpath parameter is not required.

Add the following code to manifest. MF:

Class-path: lib/Some. Jar

Lib is a sub-directory in the same directory as test. jar, where some. Jar packages to be referenced by test. jar are located.

Then test and run. Everything is normal!

If multiple jar packages need to be referenced:

Class-path: lib/Some. Jar lib/some2.jar

Separate each jar with spaces. Use relative paths.

Also, the Class-path configuration may become invalid if the META-INF contains the index. list file. Index. list is the index file generated when the jar packaging tool is packaged. deletion does not affect the operation.

Method 4: Customize classloader to load

This method is the ultimate solution. Basically, well-known Java applications are doing that, such as Tomcat and JBoss.

Http://blog.csdn.net/kesay/article/details/5807177

Four Methods for running the jar application to reference other jar packages)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.