English 中文(简体)
Running jar from different directory cannot find required dependency
原标题:

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
最佳回答

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.

问题回答

You can not use -cp and -jar together

java -cp myjar.jar;lib/*;. mypackage.MyClass

shall work on windows and

java -cp myjar.jar:lib/*:. mypackage.MyClass

shall work on Unix





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签