ClassNotFoundException in Java

ClassNotFoundException in Java is thrown if an application tries to load a class but the class with the specified name is not found.

java.lang.ClassNotFoundException

ClassNotFoundException in Java is a checked exception which means it subclasses from Exception class (not from RuntimeException).

This exception is thrown if you try to load a class at runtime through its String name using one of the following methods.

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.

But the class with the passed name is not found.

One scenario where you may encounter java.lang.ClassNotFoundException is when you try to load JDBC driver without having the require JAR in the class path. In the following example there is an attempt to load oracle driver though ojdbcXXX.jar is not in the classpath.

public class ClassNotFoundExceptionExp {
  public static void main(String[] args) {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Output

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
	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)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:332)
	at com.knpcode.programs.ClassNotFoundExceptionExp.main(ClassNotFoundExceptionExp.java:6)

As you can see Class.forName() method invocation is enclosed in try-catch block because ClassNotFoundException is a checked exception. To resolve it you need to ensure that the required jar is in the classpath.

Another scenario where you may encounter java.lang.ClassNotFoundException is when you have many third party jars in your application and those jars in turn include some other jars, in this situation it is possible that you have different versions of the same jar. For example in your application you have A.jar and B.jar and A.jar includes C.1.2.jar where as B.jar includes C.1.4.jar.

ClassNotFoundException thrown in this kind of scenario with different jars and classloaders is very difficult to resolve. It often involves going through the stack trace to look for offending jars and classes, tying to change the order in which jars are loaded, upgrading the jar version, even changing your build file to exclude some of the classes from the jar.

Related Posts

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


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.