ClassNotFoundException In Java

The java.lang.ClassNotFoundException is a checked exception in Java that occurs when the JVM tries to load a particular class but does not find it in the classpath. Since the ClassNotFoundException is a checked exception, it must be explicitly handled in methods that can throw this exception – either by using a try-catch block or by throwing it using the throws clause.

Causes of ClassNotFoundException 

  1. The class is not in the classpath:This exception is thrown when the Java Virtual Machine (JVM) tries to load a class dynamically using Class.forName() or, but the specified class cannot be found in the classpath.

  2. The class is not compiled correctly: If the class is not compiled correctly, the JVM will not be able to load it. This can happen if the class file is corrupt or if the class file is not compatible with the version of Java that you are using.

ClassNotFoundException in Java Example

A very common example of ClassNotFoundException  is when a JDBC driver is attempted to be loaded using Class.forName() and the driver’s JAR file is not present in the classpath:
				
					public class ClassNotFoundExceptionExample {
    private static final String MYSQL_DRIVER_CLASS = "com.mysql.jdbc.Driver";
    
    public static void main(String[] args) {
        try {
            System.out.println("Loading MySQL JDBC driver");
            Class.forName(MYSQL_DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            // Handle the exception
            e.printStackTrace();
        }
    }
}

				
			

Since the MySQL JDBC driver JAR file is not present in the classpath, running the above code leads to a java.lang.ClassNotFoundException:

				
					Loading MySQL JDBC driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	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:391)
	at java.base/java.lang.Class.forName(Class.java:382)
	at ClassNotFoundExceptionExample.main(ClassNotFoundExceptionExample.java:7)
				
			

Resolving ClassNotFoundException

There are a few things that you can do to resolve ClassNotFoundException:

  1. Make sure that the class is in the classpath: Find out which JAR file contains the problematic Java class. For example, in the case of com.mysql.jdbc.driver, the JAR file that contains it is mysql-connector-java.jar.Check whether this JAR is present in the application classpath. If not, the JAR should be added to the classpath in Java and the application should be recompiled.  If that JAR is already present in the classpath, make sure the classpath is not overridden (e.g. by a start-up script). After finding out the exact Java classpath used by the application, the JAR file should be added to it.

  2. Make sure that the class is compiled correctly: You can recompile the class to make sure that it is compatible with the version of Java that you are using.

  3. Make sure that the class name is correct:  You can double-check the class name to make sure that it is spelled correctly and that it is fully qualified.

  4. Use a class loader: A class loader is a way of loading classes from a different location than the classpath. You can use a class loader to load a class from a JAR file that is not in the classpath.

Preventing ClassNotFoundException

There are a few things that you can do to prevent ClassNotFoundException:

  1. Use a build tool: A build tool, such as Maven or Gradle, can help you manage your classpath and make sure that all of the classes that your application needs are in the classpath.

  2. Use a dependency management tool: A dependency management tool, such as Maven or Gradle, can help you to manage the dependencies of your application. This can help to prevent you from forgetting to include a JAR file that your application needs.

  3. Test your application thoroughly: Make sure to test your application thoroughly to make sure that it does not throw any ClassNotFoundException.