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  
%CLASSPATH% means existing classpath to be added and . points to current directory  
After setting classpath,  

java HelloWorld

**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.  
After setting classpath in linux,  

java HelloWorld

**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-Version: 3.0
Class-Path: exntension.jar

**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.
### Comments:
#### Hi, I have a jar myLib.jar which is put in tomcat...
[Anonymous]( "noreply@blogger.com") - <time datetime="2012-03-26T20:04:31.927-07:00">Mar 2, 2012</time>

I have a jar myLib.jar which is put in tomcat directory. I deployed war file in tomcat. At run time, tomcat GUI need to access a class A in myLib.jar, that's fine. But I got a NoClassDefFoundError when class A tried to access class B which is also in myLib.jar.  
How to solve this please?
<hr />
#### Thanks for asking question. At runtime class b is ...
[java enums tutorial](http://www.cloudhadoop.com/2012/02/java-enums-tutorial-best-10-examples-of.html "noreply@blogger.com") - <time datetime="2012-03-26T21:59:09.160-07:00">Mar 2, 2012</time>

Thanks for asking question.  
At runtime class b is not available. so we have to set classpath using command line or in the jar with class-path in MANIFEST.MF file  
Please give more details which tells about class path and java version, also how you are running your application. That would be helpful to solve your problem
<hr />
#### Thanks :-)
[Anonymous]( "noreply@blogger.com") - <time datetime="2012-07-10T06:50:50.876-07:00">Jul 2, 2012</time>

Thanks :-)
<hr />
#### I have a java app ( with main method ) which runs ...
[Anonymous]( "noreply@blogger.com") - <time datetime="2012-07-17T02:25:11.134-07:00">Jul 2, 2012</time>

I have a java app ( with main method ) which runs fine on windows with a -classpath parameter. On Linux, I had to set $CLASSPATH variable separately. I couldn't use the -classpath parameter. What would be a reason for this? Is something different needed on Linux than windows to run the app?
<hr />
#### Hi, i have an ear file which has jar file. During ...
[Anonymous]( "noreply@blogger.com") - <time datetime="2012-07-18T20:20:03.898-07:00">Jul 4, 2012</time>

Hi, i have an ear file which has jar file. During deployment of the ear file I see all other classes being loaded from the jar file except just one for which it gives NoClassDefFoundError. What could be the reason?
<hr />
#### thanks man ...cause of you i solved my problem in ...
[Anonymous]( "noreply@blogger.com") - <time datetime="2012-11-22T21:55:59.429-08:00">Nov 5, 2012</time>

thanks man ...cause of you i solved my problem in just a minute...
<hr />
#### Thanks
[Kiranbabu](https://www.blogger.com/profile/14122554465146445380 "noreply@blogger.com") - <time datetime="2018-09-19T05:21:23.045-07:00">Sep 3, 2018</time>

<hr />
Similar Posts