Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath Issues with java -jar option

 
Vijay Silva
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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??
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • 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
  • 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??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
paramu iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic