Mazzy
Mazzy

Reputation: 14179

NoClassDefFoundError exception after having created jar file

I have created a jar file including inside it some packages and a pair of file. This class file uses another package that I hadn't include in the jar file - I think that's the reason I get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: JigMonitorGUI (wrong name: org/w3c/jigmonitor/JigMonitorGUI)

How can I resolve this problem?

EDIT

the whole stacktrace:

Exception in thread "main" java.lang.NoClassDefFoundError: JigMonitorGUI (wrong name:  org/w3c/jigmonitor/JigMonitorGUI)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

Upvotes: 2

Views: 9498

Answers (4)

Luke Woodward
Luke Woodward

Reputation: 64949

I suspect that you haven't quite created your JAR file correctly. Your class file has a package, but it seems you're attempting to run the class not within a directory structure matching the package of the class.

Let's provide a demonstration of how you can reproduce this error message. Suppose we have the following class:

package abc.def.ghi;

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Note that this class is in the package abc.def.ghi. Suppose we save this as Hello.java in a directory C:\Users\Luke\Java and then compile it from this directory:

C:\Users\Luke\Java>javac Hello.java

We then create a JAR file using the Hello.class file:

C:\Users\Luke\Java>jar cf test.jar Hello.class

Finally, we then try to run this class from within the JAR:

C:\Users\Luke\Java>java -cp test.jar Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: abc/def/ghi/Hello)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        ...

This is the error you're getting, but with a different class name.

The problem is that the class Hello wasn't compiled within a directory structure matching its package. The JVM found the class in the root of the JAR so it expected the fully-qualified name of the class inside the class file to be Hello. However, the fully-qualified name in the class file was abc.def.ghi.Hello, which wasn't what the JVM was expecting, so it threw an error.

If we instead have Hello.java within subdirectories abc\def\ghi, matching its package abc.def.ghi, things work a little better:

C:\Users\Luke\Java>javac abc\def\ghi\Hello.java

C:\Users\Luke\Java>jar cf test.jar abc\def\ghi\Hello.class

C:\Users\Luke\Java>java -cp test.jar abc.def.ghi.Hello
Hello

EDIT: it's possible to reproduce the same errors by running the class directly, without creating a JAR. The key point is still that you're attempting to run the class from outside of a directory structure matching its hierarchy.

At this point I have Hello.class in C:\Users\Luke\Java and C:\Users\Luke\Java\abc\def\ghi:

C:\Users\Luke\Java>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: abc/def/ghi/Hello)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        ...

C:\Users\Luke\Java>java abc.def.ghi.Hello
Hello

Upvotes: 6

DankMemes
DankMemes

Reputation: 2137

Instead of invoking with java -cp whatever -jar app.jar you should edit the jar's manifest file. Something like this:

Manifest-Version: 1.0
Main-Class: abc.def.Whatever
Class-Path: jar1.jar jar2.jar jar3.jar

(And don't forget the empty line at the end of the manifest...)

Upvotes: 1

Liggy
Liggy

Reputation: 1201

The missing classes need to be on the classpath. How to do this will depend on how you want to deploy/run your application.

Upvotes: 0

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

you have to pass the missing .class file when you run your application.

java -jar -cp <path_to_missing_jars_here> yourJarName.jar

Upvotes: 1

Related Questions