简体   繁体   中英

java.lang.ClassNotFoundException when running a jar and its dependency is in the same directory

On eclipse I compiled a jar file. It uses several external archives on a mapped netwrok drive. The project runs on eclipse perfectly.

I have copied the jar + all of the dependecy jars into some Solaris directory. Then I tried

java -jar myapp.jar

and it gave me the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: confmng/ConfigManager

This class specified in the error message is inside one of the mapped jars which I have copied to the same directory as the main jar.

Also, I pinged the server where the dependency jars are located from the solaris and it was positive.

You can also see that ConfigManager class inside confmng package is really in the dependency jar:

在此处输入图片说明

What could be wrong?

Thanks

You have to specify the classpath for the dependencies, you can specify at Manifest classpath, or at the java -jar myapp.jar command with -classpath parameter.

Example:

java -classpath dependency1.jar;subfolder/dependency2.jar;myapp.jar package.of.your.main.Class

You can also export a Runnable Jar in Eclipse, so there will be some options to handle with the dependencies, like the option "Package required libraries into generated JAR".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM