How do I resolve ClassNotFoundException?

As the name suggests classNotFoundException in Java occurs when JVM (Java Virtual Machine) tries to load a particular class and doesn't found the requested class in the classpath you specified. This means that, your classpath is broken (which is a very common problem in the Java world ). This problem can be particularly confusing for Java beginners. ClassNotFoundException is a checked exception , so it has to be catch or thrown to the caller.

Java ClassNotFoundException sample

One of the most common example of ClassNotFoundException is when we try to load JDBC drivers using Class.forName but forget to add it's jar file in the classpath.
Java ClassNotFoundException

When ClassNotFoundException occurs in Java

Java ClassNotFoundException thrown when an application tries to load in a class through its string name using:

  1. The forName method in class Class.
  2. The findSystemClass method in class ClassLoader .
  3. The loadClass method in class ClassLoader.

Java ClassNotFoundException example

In the following example, there is no such class exist NoClassExist.java and try to attempt to load the class "NoClassExist".
public class Example { public static void main(String args[]) { try { Class.forName("NoClassExist"); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } } }
output
java.lang.ClassNotFoundException: NoClassExist


Java Class.forName() After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. ClassNotFoundException occurs when you try to load a class at runtime using Class.forName() or loadClass() methods and requested classes are not found in your classpath. This exception also occurs when you have two class loaders and if a ClassLoader tries to access a class which is loaded by another classloader in Java.

Exception Hierarchy


Java ClassNotFoundExceptionException Hierarchy

How to fix the ClassNotFoundException

  1. Review properly the java.lang.ClassNotFoundException stack trace which Java class was not loaded properly at runtime.

  2. Verify the name of the requested class is correct and the specified .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.

  3. If it's present in your classpath then there is high chance that your classpath is getting overridden or application is using classpath specified in jar file or start-up script and to fix that you need to find the exact classpath used by your application.

  4. In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.

Java Classpath and ClassNotFoundException

  1. Java classpath is a list of locations to load classes from. These locations can either be directories, or jar files. For directories, the Java Virtual Machine will follow an expected pattern for loading a class.
  1. For example, if you have the directory C:/java/classes in your classpath, and you attempt to load a class com.myproject.myprog, it will look under the classes directory for a directory called com, then under that a directory called myproject, and finally it will look for a file called myprog.class in that directory.
  1. In case of jar files, it will search the jar file for that class. A jar file is a zipped collection of directories like the above. If you unzip a jar file, you'll get a bunch of directories and class files following the pattern above.
  1. So the Java Virtual Machine traverses a classpath from start to finish looking for the definition of the class when it attempts to load the class definition. For example, in the classpath :
C:/myprog/classes;C:/myprog/lib/abc.jar;C:/myprog/lib/xyz.jar
  1. The Java Virtual Machine will attempt to look in the directory classes first, then in abc.jar and finally in xyz.jar.
When you get a ClassNotFoundException , it means that the Java Virtual Machine has traversed the entire classpath you specified and not found the class you've attempted to reference. The one and only solution is to check your classpath carefully.