• 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

Classpath Issues with java -jar option

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an executable jar (Main-Class specified in the manifest file), which has some runtime dependecies on a group of other jars.

When running:
java -cp lib/dependentJar1.jar;lib/dependentJar2.jar -jar myJar.jar
I always get a NoClassDefFoundError for a class in the dependent jars.

If instead I try:
java -cp lib/dependentJar1.jar;lib/dependentJar2.jar;myJar.jar MyMainClass
it works!!

I also have been able to specify the class path within my executable jar's manifest file, which also works.

Is there a reason why specifying the class path with the -jar option does not work, or am I doing something wrong??
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use

a.jar is used as source.
If the manifest contains additional jars, that is fine, but other sources specified on the commandline (and I guess in the env.-var. CLASSPATH) are ignored.
 
Vijay Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"other sources specified on the commandline (and I guess in the env.-var. CLASSPATH) are ignored"

Why is that? The Java usage (java -help) specifies:
java [-options] -jar jarfile [args...]
(to execute a jar file)

It does not mention anything about the class path option being ignored with the -jar option in the usage. This is very misleading.

Is there a reason why the classpath option is ignored?? What if a classpath is not specified in the manifest??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complete documentation for java.exe (for example, here) says,


When you use [the -jar] option, the JAR file is the source of all user classes, and other user class path settings are ignored.

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the best answer to a question I haven't asked yet in a long time! Now I can go back to work. Later ...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Silva:
Is there a reason why the classpath option is ignored?? What if a classpath is not specified in the manifest??



I can think of a reason: it gives the person who made the JAR file total control over the classpath when the JAR is executed.

If java.exe would also look at the CLASSPATH environment variable, you might get into all kinds of problems because the JRE could pick up classes that are anywhere on the system. It could cause a lot of hassle, especially with users who don't know all the internals of Java and who just want to run a Java program they get from somebody, if they have their CLASSPATH (deliberately or not) set wrong.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can give all the dependent jars in the Class-Path of the manifestfile and then run your main application using java -jar option,I guess that will work.
 
reply
    Bookmark Topic Watch Topic
  • New Topic