how to use one jar that depends on another jar
suppose in my application "MyClass", I want to use c开发者_如何学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.
I think that including jar2.jar in the jar1 MANIFEST.MF
would probably work, provided they're in the same directory.
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.
精彩评论