6 ways to fix java.lang.NoClassDefFoundError in java
This article covers a solution for What is a NoClassDefFoundError & How to fix it in Java.
We used to get the Exception in thread “key” java.lang when we ran Java programs. NoClassDefFoundError: The NoClassDefFoundError in Java is a strange error in Java applications, encountered a lot of times.
The solution to the NoClassDefFoundError is simple, but we must first understand how to solve it.
What is Java.lang.NoClassDefFoundError error in Java?
java.lang.NoClassDefFoundError
is a Runtime exception that occurs during Java program execution.
The Java program generates class files during the compilation(javac) process. It throws NoClassDefFoundError
exception when this class is not available during runtime execution.
Usually, this error occurs when the classpath is not set correctly or the dependencies jar missing in Windows, Linux, Unix, and Tomcat/Websphere/JBoss.
First, Let’s see how the Java program throws the error and fixes the NoClassDefFoundError.
public class HelloWorldDemo {
public static void main(String args[]) {
System.out.println("Hello world test message");
}
}
In the above example, This is a sample hello world program
- Compiles fine and generates the
HelloWorld.class
in the current directory by running thejavac HelloWorld.java
command. - It throws the following exception after running the
java HelloWorld
command and exits the program abnormally
Exception in thread “main” java.lang.NoClassFoundError: HelloWorld:
The reason for this error:
- 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 via the classpath,
- so the solution is to add this to the classpath
classpath
is the environment variable in every system that points to class files in the directories, Java runtime system refers to this file during execution- if your class file is in a jar file, the jar should be in a classpath, Classpath can be an
absolute
(complete path) orrelative
path( related to the directory )
Let’s see 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 supply the class file to the java
command using the -classpath
option.
This issue was resolved by setting the classpath inline for the Java command.
We are instructing the JVM to look for the HelloWorld.class
in the current directory as well as the classpath.
if the class file is in a different directory, we need to specify the complete directory absolute or relative path 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.
using set, we can set CLASSPATH in Windows using the below command.
set CLASSPATH=%CLASSPATH%;.;
%CLASSPATH%
means existing classpath to be added and .
points to the current directory
After setting a path, The program works fine.
java HelloWorld
This command works fine and prints the Hello World message.
Fix for java.lang.NoClassDefFoundError in Linux/Unix
Set path to fix this in Linux so we have to set the class or directory of the class files to a classpath
environment variable.
set $CLASSPATH=$CLASSPATH%;.;
After setting the classpath in Linux,
java HelloWorld
Output prints console 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 be configured by clicking on the project—>properties—>java build path—> select libraries for jars or classes in the 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 the 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-Title:
Implementation-Version: 3.0
Implementation-Vendor:
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
The NoClassDefFoundError
error in APpservers occurs due to the jar is not in the classpath. To fix this, see the complete stack trace find the jar
that contains the class file, 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 are thrown if the .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
.