File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java JARs With Dependancies On Other JARs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java JARs With Dependancies On Other JARs" Watch "Java JARs With Dependancies On Other JARs" New topic

Java JARs With Dependancies On Other JARs

Jason Mowat
Ranch Hand

Joined: Aug 17, 2003
Posts: 79

I have a JAR file that contains some generic base classes and interfaces.

I have a second JAR file that contains concrete implementations of the classes and interfaces from the first JAR. The second JAR also has a main class specified in the manifest of the second JAR.

Both JAR1 and JAR2 are in the same directory.

I am having classpath problems when I try to run the second jar. I have tried the following:

java -jar jar2.jar -classpath c:\path_to_dir\jar1.jar
java -jar jar2.jar -classpath .;c:\path_to_dir\jar1.jar
java -jar jar2.jar -classpath "c:\path_to_dir\jar1.jar"
java -classpath c:\path_to_dir\jar1.jar -jar jar2.jar
java -cp c:\path_to_dir\jar1.jar -jar jar2.jar

I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError:jar1_package/Jar1Class

Any suggestions on what I could be doing wrong?

Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
I'm really not a big fan of the -jar option as I think it is a little bit confusing. Try:

java -cp jar1.jar:jar2.jar

Replace the : with a ; if you are on windows.
[ December 08, 2004: Message edited by: Scott Dunbar ]

<a href="" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

In order to use the -jar option, you need to specify various settings in the jar's manifest file. See this thread for more information on executable jars.

Oh, the -jar option means that the JVM will ignore the system CLASSPATH and any classpath entered with the -cp option. Only the Class-Path specified in the manifest file (and the boot classes...) will be used.
[ December 08, 2004: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If the above suggestions still don't help, a little more information will probably be helpful for us to point you in the right direction. At the least, it would be nice to see the contents of your manifest file (if you provide one) and the contents of the two jar files. You can get the jar file contents by running the following command:

Keep Coding!


Java API Documentation
The Java Tutorial
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
One solution is to set the classpath in the manifest.MF file of the jar

Manifest-Version: 1.0
Main-Class: MainApp
Class-Path: 1.jar 2.jar 3.jar 4.jar
I agree. Here's the link:
subject: Java JARs With Dependancies On Other JARs
It's not a secret anymore!