How to Resolve Java ClassNotFoundException in 3 Easy Steps

May 6th, 2021 • By Rollbar Editorial Team

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 which can throw this exception - either by using a try-catch block or by throwing it using the throws clause.

What Causes ClassNotFoundException in Java

Common causes of the java.lang.ClassNotFoundException are using the following methods to load a class:

  • Class.forName()
  • ClassLoader.findSystemClass()
  • ClassLoader.loadClass()

In all the above cases, the class attempted to be loaded is not found on the classpath, leading to the ClassNotFoundException in Java.

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 *DRIVER_CLASS* = "com.mysql.jdbc.Driver";

    	__public static void__ main(String[]  args) __throws__ Exception {
        		System.*out*.println("Loading MySQL JDBC driver");

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
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
	at java.base/java.lang.ClassLoader.loadClass(
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(
	at ClassNotFoundExceptionExample.main(

To fix the Java exception, the mysql-connector JAR should be included in the application classpath.

How to Resolve ClassNotFoundException in Java

The following steps should be followed to resolve a ClassNotFoundException in Java:

  1. 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.
  2. 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.
  3. 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.

Track, Analyze and Manage Java Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates Java error monitoring and triaging, making fixing Java errors easier than ever. Try it today

Get the latest updates delivered to your inbox.