Java – How to Fix java.lang.NoClassDefFoundError?

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
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Java. Tagged with .

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *