简体   繁体   中英

how to use one jar that depends on another jar

suppose in my application "MyClass", I want to use class A in jar1, and some part of class A depends on some class B in jar2. If I put both jar1 and jar2 on my classpath and run MyClass, it still complains that class B can not be found.

Could anybody tell me how to fix this problem?

Thanks a lot.

The %CLASSPATH% environment variable is ignored when you use java command with -jar argument. Specify it using -classpath argument or its shorthand -cp . Alternatively you can also specify it in the class-path entry of the MANIFEST.MF file of the JAR file which you execute.

I have never encountered this being a problem, but have you tried putting jar2 before jar1 in the class path order? I think it's more likely that jar2 is simply not being included properly. Can you post the classpath setting?

Also, you may want to open up jar2 (it's just a zip file), and verify that the class you think is in there is in fact in there.

我认为将jar2.jar包含在jar1 MANIFEST.MF可能会起作用,只要它们位于同一目录中即可。

As other answers have mentioned providing the name of needed jar in the MANIFEST.MF file of the jar that you invoke is one way to resolve your problem.

During jar file (the one you invoke first) creation you can specify the manifest file which contains this detail so that it gets bundled with your jar.

The Java tutorial has a section on how to specify the Manifest file during the jar creation.

If you are using Ant as your build tool you can use the Ant task Manifest to create a manifest file. You can use the Manifest file thus created in the Ant task that builds your jar.

Also check this link which provides more details about the Manifest file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM