Running jar from different directory cannot find required dependency

classpath, dependencies, jar, java

Solution

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.

Problem

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 ```

Original source