Search code examples
javaeclipsejarnoclassdeffounderrorclassnotfoundexception

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:

enter image description here

What could be wrong?

Thanks


Solution

  • 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".