Java – How to Fix java.lang.NoClassDefFoundError?

1
This article represents tips on How to Fix java.lang.NoClassDefFoundError when compiling a particular Java file. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • How to reproduce java.lang.NoClassDefFoundError?
  • Why does the java.lang.NoClassDefFoundError occur in the first place?
  • How to Fix the Error?
How to reproduce java.lang.NoClassDefFoundError?

Take a look at following Class file.

package com.test;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World! How are you?");
    }
}

Following are steps to reproduce the java.lang.NoClassDefFoundError.

  • Save the above file as HelloWorld.java within any folder.
  • Compile the file using javac command using following: “javac HelloWorld.java”
  • Start the JVM using command such as “java HelloWorld”.
  • Following error would show up:
    java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/test/HelloWorld)
    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)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    

 

Why does the java.lang.NoClassDefFoundError occur in the first place?

Following happens when you start JVM using “java HelloWorld.java”:

  • JVM tries to find “HelloWorld” class file within the cache. If found, the class loading is done which is then followed by linking, initialization and method execution.
  • If the class is not found in the cache, JVM follows the delegation model to find the class. In this model, the “AppClassLoader” delegates the parent classloader (Extension classloader) to find the class (in ext directory) which then delegates the activity to BootClassLoader to find the class in %JAVA_HOME%/hre/lib/rt.jar.
  • If not found, the AppClassLoader tries to find the class in current folder. However, given the package information, “com.test”, the class is expected to find in “com\test” folder.

 

How to fix the java.lang.NoClassDefFoundError?

Take a look at following Class file.

package com.test;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World! How are you?");
    }
}

Do following to print “Hello World! How are you?”:

  • Create folder com/test
  • Place the following file, HelloWorld.java in com/test folder.
  • Compile the file from top most folder consisting of com folder using following command: “javac com\test\HelloWorld.java”
  • From topmost folder consisting of com folder, execute the following command, “java com.test.HelloWorld”. And, it should be fine.
Ajitesh Kumar

Ajitesh Kumar

Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, .NET, C/C++, mobile programming languages etc and, computing fundamentals such as application security, cloud computing, API, mobile apps, google glass, big data etc.Recently, he has been digging deep into the field of data science and machine learning.

Follow him on Twitter and Google+.
Ajitesh Kumar

1 Comment

  1. AWS does allow you to set up billing alerts. Amazon CloudWatch allows you to use your projected monthly bill as a metric for alerts. You can have a notification sent to you when it exceeds a preset dollar amount.

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.