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

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

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

3 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago