This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 1817

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
It is sorta covered in the JavaRanch Style Guide.
subject: Java JARs With Dependancies On Other JARs
Similar Threads
jar versioning
Access class files between two jar files
Archive problem.
Access class files between two jar files