Tutorials Logic, IN info@tutorialslogic.com

ClassNotFoundException in Java: Causes, Fixes, Examples & Interview Tips

What is This Error?

The ClassNotFoundException is a checked exception thrown when the JVM tries to load a class at runtime using Class.forName(), ClassLoader.loadClass(), or ClassLoader.findSystemClass(), but cannot find the class definition in the classpath.

Common Causes

  • Required JAR file not added to the classpath
  • Typo in the fully qualified class name
  • Class exists in a different version of the JAR
  • Dependency not included in Maven/Gradle build
  • Class was removed or renamed in a library update

Quick Fix (TL;DR)

Quick Solution

Quick Solution
<!-- Add missing dependency to pom.xml (Maven) -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

Common Scenarios & Solutions

Problem

Problem
// ClassNotFoundException: com.mysql.jdbc.Driver
Class.forName("com.mysql.jdbc.Driver"); // JAR not in classpath!

Solution

Solution
<!-- Maven pom.xml -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

<!-- Also update class name for MySQL 8+ -->

Updated Driver Class

Updated Driver Class
// Old (MySQL 5.x)
Class.forName("com.mysql.jdbc.Driver");

// New (MySQL 8+)
Class.forName("com.mysql.cj.jdbc.Driver"); // Updated class name

Solution

Solution
// build.gradle
dependencies {
    implementation 'mysql:mysql-connector-java:8.0.33'
    implementation 'org.springframework:spring-core:6.0.0'
}

// After adding, run:
// gradle build  or  ./gradlew build

Solution

Solution
# Compile with classpath
javac -cp .:lib/mysql-connector.jar Main.java

# Run with classpath
java -cp .:lib/mysql-connector.jar Main

# Windows (use ; instead of :)
java -cp .;lib\mysql-connector.jar Main

Solution

Solution
try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    System.out.println("Driver loaded successfully");
} catch (ClassNotFoundException e) {
    System.err.println("MySQL driver not found: " + e.getMessage());
    System.err.println("Add mysql-connector-java to your classpath");
    // Log and handle gracefully
}

Best Practices to Avoid This Error

  • Use Maven or Gradle - Dependency management handles classpath automatically
  • Check class names carefully - Fully qualified names are case-sensitive
  • Verify JAR versions - Class names can change between library versions
  • Use IDE dependency management - IntelliJ/Eclipse show missing dependencies
  • Always catch ClassNotFoundException - It's a checked exception
  • Run mvn dependency:tree - Check for missing or conflicting dependencies
  • Check library migration guides - Class names change in major versions

Related Errors

Frequently Asked Questions

ClassNotFoundException is a checked exception thrown when loading a class dynamically at runtime. NoClassDefFoundError is an error thrown when a class was available at compile time but not at runtime.

Use -cp flag: java -cp .:lib/myjar.jar Main. In Maven, add a dependency in pom.xml. In Gradle, add to dependencies block. In IDEs, right-click project → Add to Build Path.

The MySQL JDBC driver JAR must be in the classpath. Also, MySQL 8+ changed the driver class from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver.

Print System.getProperty("java.class.path") in your code. Use mvn dependency:tree for Maven projects. Use gradle dependencies for Gradle projects.

Yes, it's a checked exception so you must either catch it or declare it with throws. Always handle it gracefully with a meaningful error message.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.