Six ways to fix java.lang.NoClassDefFoundError in java


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

fix for NoClassDefFoundError in java

The NoClassDefFoundError in Java is a strange error that can occur in java application.

We used to get the Exception in thread “key” java.lang when we ran java programmes. 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 a number of circumstances, including Windows, Linux, Unix, and Tomcat/Websphere/JBoss.

I’ll explain how java programme throws the error and how to 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 programme that compiles fine and generates HelloWorld.class in the current directory during running javac HelloWorld.java command.

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

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

thread main throws this error and exit 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 you class file is in jar file, jar should be in classpath. class path 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 is occurred and fix 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 -classpath option

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

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

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

java -classpath . HelloWorld

Fix for java.lang.NoClassDefFoundError in windows

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

to set class path in windows, we have to configure the below values

set CLASSPATH=%CLASSPATH%;.;

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

After setting path,

java HelloWorld

This command works fine and prints hello world message

Fix for java.lang.NoClassDefFoundError in linux/unix

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

set $CLASSPATH=$CLASSPATH%;.;

After setting classpath in linux,

java HelloWorld

command works fine and prints hello world message

Fix for java.lang.NoClassDefFoundError in eclipse

java.lang.NoClassDefFoundError in eclipse usually occurs that your project is missing some of required jars in your build path.
we have to first configure your project build path.
we can configure by right click on project–>properties–>java build path–> select libraries for jars or for classes in folder select source..

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

and also we found that when we are running java class file presented in jar file, java.lang.NoClassDefFoundError error occurs, sho 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-Title:   
Implementation-Version: 3.0  
Implementation-Vendor:   
Class-Path: exntension.jar  

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 of the jar is not in classpath. To fix this, see the complete stack trace and find the jar which contains jar and tries to copy in 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 looks similar and related to classpath only but ClassNotFoundException exception throws application tries load the class using name of the class or class calling other class which 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 load in parent class is not available for 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,
where as ClassNotFoundException error occurs if 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 about comparison of ClassNotFoundException with NoClassDefFoundError.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.