Categories: Java

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

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

View Comments

  • 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.

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

24 hours ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

2 days ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

2 days ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

3 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

3 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

3 days ago