I created two jar files, my.common.jar which contain helper classes and methods (mostly static methods). I also created a jar file, test.jar with a main method that calls a static method in a class in my.common.jar.
Everything works fine when I launch main like this:
java -classpath path/to/myjars/my.common.jar:./test.jar test.Tester
Tester is the class in test.jar that contains method main.
But I get NoClassDefFoundError my/common/Myclass when I run it this way:
java -classpath path/to/myjars/my.common.jar -jar test.jar
I tried so hard, but I am not able to figure out why it fails or how to resolve this issue. I do appreciate your help.
—– addendum —-
I forgot to mention that the manifest file in test.jar looks like so:
Manifest-Version: 1.0
Build-Jdk: 1.6.0_13
Created-By: Apache Maven
Main-Class: test.Tester
Archiver-Version: Plexus Archiver
You cannot use both
-classpathand-jaroptions on the command line. Use one or the other … but not both.When you use the
-jaroption, the command expects to find the entry point class AND the runtime classpath in the JAR file manifest. Your-classpathargument will be ignored, as will the CLASSPATH environment variable. To quote from thejavamanual page:If your (executable) JAR file needs to use things in other JARs, then it need a
Class-Pathattribute in the manifest. This attributes value is a space separated list of URLs; see here.So in your case, you would need to add something like this to your JAR’s manifest:
This page of the Java Tutorial covers this topic, and includes the warning that the
Class-pathmanifest line must be properly terminated with a CR or NL for the JVM to recognize it.