Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java JARs With Dependancies On Other JARs

 
Jason Mowat
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

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
etc...

I get the following error:

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

Any suggestions on what I could be doing wrong?

Thanks,
Jason
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 the.class.name.that.has.your.main

Replace the : with a ; if you are on windows.
[ December 08, 2004: Message edited by: Scott Dunbar ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Layne
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic