Artikel Terbaru

Tuesday, 6 March 2012

6 ways to resolve java.lang.NoClassDefFoundError in java

How to fix for java.lang.NoClassDefFoundError error in java


fix for NoClassDefFoundError in java
NoClassDefFoundError in java is one of the strange error used to get in java applications.
Usually when we run java programs, we used to get the Exception in thread “main” java.lang.NoClassDefFoundError: in our programs I got this exception alot of times. Solving this NoClassDefFoundError is very easy but we have to know the ways to solve this. NoClassDefFoundError exception will be in different cases like windows,linux,unix and tomcat/websphere,jboss. I am going to explains about the sample program assumes that which throws NoClassDefFoundError  and fix for this error.


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

This is sample hello world program,When i compile this program using javac HelloWorld.java command, this compiles fine and generates HelloWorld.class in the current directory,

After running this programm using java HelloWorld command, I am getting the below exceptions.

Exception in thread "main" java.lang.NoClassFoundError: HelloWorld


thread main throws this error and exit the program abnormally.

This reason for this error is java virtual machine can not find class file at run time. java command looks for the classes that are there in the current directory, so if your class file is not in current directory, you have to set in classpath, so the solution is to place this .class file in the classpath

classpath is the enviornment variable in every system which points to class files in the directories. if you classfile is in jar file, jar should be in classpath. classpath can be absolute(complete path) or relative path( related to directory )


Many ways to solve java.lang.NoClassDefFoundError exception.
Following are the different cases where this error is occured and fix for them.

1. solve java.lang.NoClassDefFoundError :-

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

java -classpath . HelloWorld

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

2. Fix for java.lang.NoClassDefFoundError in windows:-

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

to set classpath 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 classpath,
java HelloWorld
command works fine and prints hello world message

3. Fix for java.lang.NoClassDefFoundError in linux/unix:-

This is like setting classpath 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

4. Fix for java.lang.NoClassDefFoundError in eclipse:-
java.lang.NoClassDefFoundError in eclipse usually ocurrns 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..

5. 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 classpath, so we have to set the jar to the classpath

5. 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 similiar 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.ClassNotFound Exception : classnot found exception is thrown when classes are loaded by different classloaders at runtime. here child class which load in parent class is not available for class loader to load the class. then 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.