Run a .jar file on Unix always resolves in a NoClassDefFoundError Exception

I am working on a little java program which I want to run on a unix machine.

First, I compiled the .java file without any errors.

javac CallACMDFromJAVA.java

Then my .class file was created.

CallACMDFromJAVA.class

Then I used the jar command to create an executable .jar file.

jar cfm cmd.jar manifest.txt CallACMDFromJAVA.class

So far so goo. The last cmd gave me a cmd.jar file. But when I want to run it, some error suddenly appears. I typed:

java -jar cmd.jar

The Error:

Exception in thread "main" java.lang.NoClassDefFoundError: CallACMDFromJAVA (wrong name: serviceCall/CallACMDFromJAVA)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

Do you need more information about java environment or something else to help me?

Jon Skeet
people
quotationmark

Your class is apparently in a package serviceCall. Ideally, you should make your source directory structure match the package structure, so you have the source file in a directory called serviceCall. Even without that, if you compile it with

javac -d . CallACMDFromJAVA.java

then that should create a serviceCall directory containing CallACMDFromJAVA.class. You should try to run this directly before putting it in a jar file:

java serviceCall.CallACMDFromJAVA

... that should work, whereas java CallACMDFromJAVA would fail with the same error using your existing workflow. It's always worth trying it without the jar file before putting it in a jar file.

You then need to change your Main-Class in the manifest to serviceCall.CallACMDFromJAVA, and create the jar file to include the serviceCall directory, e.g.

jar cfm cmd.jar manifest.txt serviceCall

Then it should work. Basically, you need your Main-Class attribute to refer to the fully-qualified class name, and you need the corresponding class file to be in a suitable directory within the jar file.

Note that if you use a good IDE, all of the directory structuring is likely to be handled for you.

people

See more on this question at Stackoverflow