views:

327

answers:

1

I'm trying to run a jar ec/mobat/MOBAT.jar which depends on some jars located in ec/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
+3  A: 

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.

Thomas Jung
I 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.
Alexandru
Did you check with jar xft <jar> that the class is in any of the jars?
Thomas Jung
Please post the full command line with "..." filled in.
Thomas Jung
http://pastebin.com/m2d1ac730
Alexandru
@Alexandru - See edit.
Thomas Jung
I see, but still, I fail to understand why `ec/mobat$ java -jar MOBAT.jar` find the other jars.
Alexandru
Your answers translate to: I CAN ONLY RUN A JAR LOCATED IN THE CURRENT DIRECTORY! I really hope that's not the case
Alexandru
And another thing: I tried to modify the MANIFEST.MF but now I get `java.io.IOException: line too long`
Alexandru
No, 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
> 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." http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Name-Value%20pairs%20and%20Sections
Thomas Jung
Could you fix the problem?
Thomas Jung
Not really. I resorted to running the jar from its directory and I didn't bother, but your answer clarified some things at least.
Alexandru