Six ways to fix java.lang.NoClassDefFoundError in java

This article covers a solution for How to fix a NoClassDefFoundError in Java.

The NoClassDefFoundError in Java is a strange error in Java applications.

We used to get the Exception in thread “key” java.lang when we ran java programs. NoClassDefFoundError:

I’ve seen this exception a lot of times in java applications.

The solution to the NoClassDefFoundError is simple, but we must first understand how to solve it.

The NoClassDefFoundError exception can exist in many circumstances, including Windows, Linux, Unix, and Tomcat/Websphere/JBoss.

I’ll explain how the java program throws the error and fix the NoClassDefFoundError.

public class HelloWorldDemo {
 public static void main(String args[]) {
  System.out.println("Hello world test message");

This is a sample hello world program that compiles fine and generates the HelloWorld.class in the current directory during running the javac command.

I’m getting the following exceptions after running this program with the java HelloWorld command.

Exception in thread “main” java.lang.NoClassFoundError: HelloWorld:

thread main throws this error and exits the program abnormally.

The reason for this error is that the java virtual machine cannot locate a class file during runtime. The java command checks for classes in the current directory, so if your class file isn’t in the current directory, you’ll have to add it to the classpath, so the solution is to put this. In the classpath, there is now a class file.

classpath is the environment variable in every system which points to class files in the directories.

if your class file is in a jar file, the jar should be in a classpath. Classpath can be absolute(complete path) or relative path( related to directory )

Multiple ways to solve java.lang.NoClassDefFoundError exception.

Following are the different ways where this error occurs and fixes for them.

How to solve java.lang.NoClassDefFoundError

HelloWorld.class is not available at runtime, so we have to set the class file to java command using -the classpath option.

It is for fixing the NoClassDefFoundError error by setting classpath inline for the java command.

We are instructing the JVM to look for the HelloWorld.class in the current directory by specifying.

if the class file is in a different directory, we need to specify the complete directory absolute or relative path instead. for the java command

java -classpath . HelloWorld

Fix for java.lang.NoClassDefFoundError in windows

To solve the NoClassDefFoundError error in windows, we have to set a CLASSPATH environment variable.

to set classpath in windows, we have to configure the below values


%CLASSPATH% means existing classpath to be added and. points to the current directory

After setting a path,

java HelloWorld

This command works fine and prints the hello world message.

Fix for java.lang.NoClassDefFoundError in linux/unix

It is like setting a path in Linux
so we have to set the class or directory of the class files to a classpath environment variable.


After setting classpath in Linux,

java HelloWorld

the command works fine and prints a hello world message

Fix for java.lang.NoClassDefFoundError in eclipse

java.lang.NoClassDefFoundError in eclipse usually occurs when your project is missing some of the required jars in your build path.

First, configure your project build path.
It can configure by right click on the project—>properties—>java build path—> select libraries for jars or classes in folder select source.

Fix for java.lang.NoClassDefFoundError error for jar files:

and also we found that when we are running a java class file presented in a jar file, java. lang.NoClassDefFoundError error occurs, so you have to set classpath in MANIFEST.MFin eclipse:-

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.6.0-beta2-b86 (Sun Microsystems Inc.)
Implementation-Version: 3.0
Class-Path: exntension.jar

The class-path attribute value jar is missing in your path, so we have to set the jar to the classpath.

Fix for java.lang.NoClassDefFoundError error in tomcat,JBoss,WebSphere*

NoClassDefFoundError error in servers occurs because the jar is not in the classpath. To fix this, see the complete stack trace and find the jar which contains the jar and try to copy it to the server classpath or application classpath. Please leave a comment if you are seeing any issues with servers.

we have seen that NoClassDefFoundError throws at runtime but what about java.lang.ClassNotFoundException error. both look similar and related to classpath only but the ClassNotFoundException exception throws the application trying to load the class using the name of the class or class calling another class that is not loaded.

what is java.lang.ClassNotFoundException :**

ClassNotFoundException is thrown when classes are loaded by different class loaders at runtime.

here child class which loads in the parent class is not available for the class loader to load the class.

what is different between java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException? we will see in the below paragraph

Difference between ClassNotFoundException and NoClassDefFoundError?

Both are unable to load at run time,
NoClassDefFoundError errors thrown if .class file cannot be found,
whereas the ClassNotFoundException error occurs only when you try to load a class by string name by ClassLoader using class.forName or Class loader’s findSystemClass method or LoadClass in ClassLoader.

Hope you understand the fix to solve NoClassDefFoundError in java. Please leave a comment and share this.

Wrap up

There are many ways we can fix this error in different environments and also discussed a comparison of ClassNotFoundException with NoClassDefFoundError.