aspose file tools*
The moose likes Java in General and the fly likes Classpath Issues with java -jar option Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classpath Issues with java -jar option" Watch "Classpath Issues with java -jar option" New topic
Author

Classpath Issues with java -jar option

Vijay Silva
Greenhorn

Joined: May 11, 2006
Posts: 3
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

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Vijay Silva
Greenhorn

Joined: May 11, 2006
Posts: 3
"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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
paramu iyer
Greenhorn

Joined: May 13, 2006
Posts: 24
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath Issues with java -jar option