5 Tips to Fix Exception in thread "main" java.lang.NoClassDefFoundError in Java, Examples

NoClassDefFoundError in Java
The Exception in thread "main" java.lang.NoClassDefFoundError is a common error in Java that occurs if a ClassLoader cannot find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running the Java application. This error can occur to any thread, but if it happens in the main thread, then your program will crash. As per Javadoc, NoClassDefFoundError can be thrown during the linking or loading of the class file. 

It's denoted by java.lang.NoClassDefFoundError and comes when that particular class is present during compile time but somehow not available during runtime. This could be due to a missing JAR file, any permission issue, or an incorrect classpath on runtime, depending upon your environment.

Java Programmers often confuse between java.lang.ClassNotFoundException and java.lang.NoClassDefFoundError in Java is also related to class loading in Java but there is a slight difference between NoClassDefFoundError and ClassNotFoundException in Java.

The ClassNotFoundException comes when Java tries to load a class by its binary name during runtime, and Class is not available at that time. For example, when you load JDBC driver using Class.forName(String classname), you will get java.lang.ClassNotFoundException instead of NoClassDefFoundError.

On the other hand, NoClassDefFoundError comes when the class was available at compile time but somehow not available at runtime. If the static initializer block of a class throws an Exception during loading, the class will not be available for others to use. 

If any class tries to refer to that class, the classloader will throw the java.lang.NoClassDefFoundError. If you like, you can read more about their difference in this article.




5 Tips to Fix  Exception in thread "main" java.lang.NoClassDefFoundErro in Java for Beginners

The Exception in thread "main" java.lang.NoClassDefFoundError in Java can come due to various reasons in Java, But most of the time, java.lang.NoClassDefFoundError comes due to CLASSPATH errors in Java. 

A correct understanding of how classpath works in Java is required to troubleshoot and solve the  Exception thread "main" java.lang.NoClassDefFoundError in Java.

Here are some common reasons that have caused Exceptions in thread "main" java.lang.NoClassDefFoundError in various Java applications in the past, your problem could well be due to any of these reasons. 

1. Missing JAR file in Classpath 

The offender class is not available in the classpath of your Java application. For example, if you are getting "Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger" error in your application, classloader of your application is not able to find org.apache.log4j.Logger class in its classpath. 

The simplest reasons for this error are the missing log4j.jar file in the classpath. As soon as you add that JAR into the classpath, the error goes away. See here to learn more about troubleshooting this particular issue.

Similarly, you might have seen java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject which comes if you are using XMLbeans but xmlbeans-2.60.jar is not present in your classpath (see here).


Another common one is java.lang.NoClassDefFoundError: org/dom4j/DocumentException, which comes due to missing dom4j.jar file (see here).

In short, the first step to solving "Exception in thread "main" java.lang.NoClassDefFoundError" is to check if the offender class is available in classpath or not. In Eclipse, you can use shortcuts like search resource (Ctrl + R) or search type (Ctrl + T) to find which JAR file that particular class belongs to and then check if that JAR is available in the classpath or not. See here to learn more useful Eclipse shortcuts for Java developers.


2. Due to the Visibility issues between multiple Classloaders

Sometimes in complex environments, e.g., Java EE container, the offender cases may be available in classpath but not visible to the classLoader trying to load that particular class. For example, in Java classloader works by delegation and visibility. 

A class loading request is first delegated to the parent class loader before the child class loader attempts to load it; hence any class loaded by the parent class loader is visible to the child class loader, but the opposite is not true, i.e., a class loaded by child classloader is not visible to parent class loader.

Suppose a class is used by two JAR files, e.g., WAR and EJB-JAR files, but present in the WAR file; then, it will be visible to the web app classloader but not the classloader, which loads the class from the EJB-JAR file.


3. Missing classpath entry in Manifest file

You can also get the "Exception in thread "main" java.lang.NoClassDefFoundError" if you are running your Java application using java -jar command and that class were not defined in manifest file's ClassPath attribute. 

In the past, I have spent hours figuring out which JAR is missing, only to find that Java is using the classpath defined in the manifest file and not the classpath environment variable.

Exception in thread "main" java.lang.NoClassDefFoundError - Cause and solution



4. Environment Issues 

I have seen strange reasons of java.lang.NoClassDefFound error while working in Java. In one case, one of the shell scripts which was starting our Java application was overriding the value of the CLASSPATH environment variable. 

Due to this reason, it's not a good idea to use the classpath environment variable for running a Java application. 

Instead, you should define the classpath with a list of JAR files in the start script and start your Java application using the java -classpath command. See here to learn more about how to add multiple JAR files in the classpath.



5. Error in Static Initializer block

One of the most common reasons for "Exception in thread "main" java.lang.NoClassDefFoundError" is the error in the static initializer block.  This is the block of code that is declared using static keywords and executed when a class is loaded. I suppose class A throws Exception from the static initializer block, and class B uses class A. In that case, when class B will be loaded, it will throw "Exception in thread "main" java.lang.NoClassDefFoundError" because it will not be able to find it's a dependency which is class A. 

So next time, if you see the NoClassDefFound error in your Java application, make sure you search for static initializer error, i.e., java.lang.ExceptionInInitializerError in your log file. That is the root cause of the NoClassDefFoundError you were seeing.


That's all on NoClassDefFoundError in Java and the common reason which causes Exceptions in thread "main" java.lang.NoClassDefFoundError in Java. Most of the time, it's just a missing JAR file that causes this problem, but sometimes it can be a nightmare to solve, like in case your application is picking classpath from the manifest file, and you are checking classpath entry somewhere else.

To deal with java.lang.NoClassDefFoundError, two most important things, is knowledge of Classpath and ClassLoader in Java. So, you should invest some time in it. To start with, you can read Java Puzzlers by Joshua Bloch.


5 comments:

  1. What is difference between NoClassDefFoundError and a ClassNotFoundException? I guess both comes due to missing class in CLASSPATH? Why Java has two types of error for same reasons?

    ReplyDelete
  2. NoClassDefFoundError comes when you use new operator and NoClassFoundException comes when you use newInstance() method i.e. trying to create object at runtime.Both are different.

    ReplyDelete
  3. Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/chrome/ChromeDriver
    at Laptop.lenovo.main(lenovo.java:10)

    ReplyDelete
  4. Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: com/google/inject/Stage

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.