• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java JARs With Dependancies On Other JARs

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic