Blog |

How to Fix ClassNotFoundException in Java

How to Fix ClassNotFoundException in Java
Table of Contents

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.

Install the Java SDK to identify and fix exceptions

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.

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

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 errors easier than ever. Try it today.

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape