September 12, 2022

NoClassDefFoundError in Java

In this post we’ll discuss about java.lang.NoClassDefFoundError and how to fix it.

When is NoClassDefFoundError in Java thrown

NoClassDefFoundError in Java is thrown when a class is present at compile time but at runtime when the JVM or a ClassLoader instance tries to load in the definition of a class it is not found.

Note that NoClassDefFoundError is a descendant of java.lang.Error. Since it is of type Error so you can’t do any exception handling to recover from it.

NoClassDefFoundError Java Example

Let’s say I have a program to read a file into a byte array that uses commons.io.FileUtils for that commons-io—xxx.jar is included in the classpath. Program compiles and executes with out any problem.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class FileToByteArray {
  public static void main(String[] args) {
    File file = new File("F:\\knpcode\\links.txt");        
    readToByteArrayUsingCommons(file);  
  }
	
  private static void readToByteArrayUsingCommons(File file){
    try(FileInputStream fis = new FileInputStream(file)) {
      byte[] bArray = IOUtils.toByteArray(fis);
      for (int i = 0; i < bArray.length; i++){
        System.out.print((char) bArray[i]);
      }
      bArray = FileUtils.readFileToByteArray(file);
      for (int i = 0; i < bArray.length; i++){
        System.out.print((char) bArray[i]);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Now you move the code classes to another server but somehow the required jar commons-io—xxx.jar is missed. Then you will get this error because now at runtime required IOUtils class is not there.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
        at com.knpcode.programs.FileToByteArray.readToByteArrayUsingCommons(FileToByteArray.java:18)
        at com.knpcode.programs.FileToByteArray.main(FileToByteArray.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 2 more

As you can see from the stacktrace, NoClassDefFoundError is thrown as a result of ClassNotFoundException. Another reason for NoClassDefFoundError is ExceptionInInitializerError when an exception occurs during evaluation of a static initializer or the initializer for a static variable.

In the following example there is a class ABC with a static block which will throw exception because of division by zero. In the main method two instances of the ABC class are created. First initialization attempt throws ExceptionInInitializerError and the second attempt results in NoClassDefFoundError because JVM already knows that the instance of ABC class can’t be created.

public class NoClassDef {

  public static void main(String[] args) {
    try {
      ABC obj1 = new ABC();
    }catch (Throwable e) {
      e.printStackTrace();
    }
    System.out.println("in here");
    // Trying to initialize again
    ABC obj2 = new ABC();
  }
}

class ABC{
  static {
    int i = 1/0;
  }
}
Output
java.lang.ExceptionInInitializerError
	at com.knpcode.programs.NoClassDef.main(NoClassDef.java:7)
Caused by: java.lang.ArithmeticException: / by zero
	at com.knpcode.programs.ABC.(NoClassDef.java:18)
	... 1 more
in here
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.knpcode.programs.ABC
	at com.knpcode.programs.NoClassDef.main(NoClassDef.java:12)

While resolving NoClassDefFoundError you will have to look for the root cause which ultimately results in NoClassDefFoundError being thrown.

That's all for the topic NoClassDefFoundError in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment