Running jar from different directory cannot find required dependency
The classpath has to contain every jar you're depending on.
java -classpath b.jar;c.jar -jar a.jar //does not work see below
The ";" is system dependent for windows ":" for unix.
The jar switch is used to select the jar file whose main class is executed (Main-Class: mobat.Launcher in the manifest file). The command line:
java -classpath b.jar;c.jar;a.jar mobat.Launcher
Would produce the same result.
Alternatively classpath definitions can be added to the Manifest file. Your manifest file could contain the attribute.
Class-Path: lib/b.jar lib/c.jar
Then
java -jar a.jar
would work.
Edit:
I thought that -jar and -cp could be used together. But the java tools documentation is clear:
-jar
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
Only the manifest and everything explict (classpath and main class) versions work.
Related videos on Youtube
Comments
-
Alexandru almost 2 years
I'm trying to run a jar
ec/mobat/MOBAT.jar
which depends on some jars located inec/mobat/lib/
. It works if I do:ec/mobat/$ java -jar MOBAT.jar
However I want to be able to run the jar from another directory
ec/$ java -jar mobat/MOBAT.jar
But I get an exception
java.lang.NoClassDefFoundError: ibis/io/Serializable ...
I tried to pass the required jars in the classpath
ec/$ CLASSPATH=... java -jar mobat/MOBAT.jar ec/$ java -jar -cp ... mobat/MOBAT.jar
but I get exactly the same exception. Any fix?
Update: MANIFEST.INF contains the following:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.0 Created-By: Selmar Kagiso Smit Main-Class: mobat.Launcher Implementation-Version: 1.3.4
-
Thomas Jung over 14 yearsCould you post the Manifest of the MOBAT.jar? Does it define some dependencies to the jars in the lib folder?
-
-
Alexandru over 14 yearsI don't want to run the class or to modify the jar (it's a library, not my sources) so your 2nd and 3rd solutions don't work (I actually tried the 2nd, but got a different error). As I explained in the question, I already tried the first solution: passing jars in the classpath but still didn't work.
-
Thomas Jung over 14 yearsDid you check with jar xft <jar> that the class is in any of the jars?
-
Alexandru over 14 yearsI see, but still, I fail to understand why
ec/mobat$ java -jar MOBAT.jar
find the other jars. -
Alexandru over 14 yearsYour answers translate to: I CAN ONLY RUN A JAR LOCATED IN THE CURRENT DIRECTORY! I really hope that's not the case
-
Alexandru over 14 yearsAnd another thing: I tried to modify the MANIFEST.MF but now I get
java.io.IOException: line too long
-
Thomas Jung over 14 yearsNo, you can run the jar from any directory. I must say that I have no explanation why it works in the directory. This should not work. The other jars are missing. You said in the beginning that you're using the $CLASSPATH variable. Maybe the envorinment variable is still used. Maybe that is not a "user class path setting" for the vm.
-
Thomas Jung over 14 years> line too long: Manifest file attributes are limitted in the line length. "Binary data of any form is represented as base64. Continuations are required for binary data which causes line length to exceed 72 bytes." java.sun.com/j2se/1.4.2/docs/guide/jar/…
-
Alexandru over 14 yearsNot really. I resorted to running the jar from its directory and I didn't bother, but your answer clarified some things at least.