Java dozen jar packages, referencing other. jar files

Source: Internet
Author: User
Java dozen jar packages, referencing other. jar Files 2009-06-01 14:39

Everyone knows that a Java application project can be packaged into a jar, and of course you have to specify a main class with a main function as the program entry for your jar package.

The specific method is to modify the Manifest.mf file in the Meta-inf of the jar package.

For example, there is a jar bag called Test.jar, which has a main class:test.someClassName with a main function.

We just add the following sentence in the MANIFEST.MF:

Main-class:test.someclassname

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

But our project needs to refer to other third party jar packages, and in eclipse we refer to this package called Some.jar in the form of a project jar package, which was placed in the Lib subdirectory of the project, and the last project was packaged with the Some.jar, but with Java- The jar was unable to find the class exception when executing this test.jar because the jar reference does not have a jar package that is placed inside itself.

So what to do.

The way it is added to the classpath at run time is not OK. is to add the Classpath parameter while running the jar:

Java-classpath Some.jar-jar Test.jar

This way should be able to solve the problem, try, or not.

Check the data, the original use of the Java-jar command to run the jar package when the classpath parameters will be invalidated, so this way is not.

So how do you refer to the other jar packs, the answer is in the MANIFEST.MF file under this directory Meta-inf. The information that the jar package references other jar packages must be declared in this file.

We add the following code to the MANIFEST.MF:

Class-path:lib/some.jar

Lib is a subdirectory of the same directory as Test.jar, Test.jar the Some.jar package to be referenced here.

Then the test runs and everything works.

If you have multiple jar packages that need to be referenced:

Class-path:lib/some.jar Lib/some2.jar

Each individual jar can be separated by a space.

Summary: Anyway, just find a way to import the jar that needs to be referenced before executing Test.jar classpath. You can even put all the packages you need to refer to the Jre/lib/ext directory so that the JVM is loaded as soon as it starts.

Related Article

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.