Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

ClassNotFoundException in Java — How to Fix (2026) | Tutorials Logic

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
<!-- 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

Scenario 1: Missing JDBC Driver

Problem
// ClassNotFoundException: com.mysql.jdbc.Driver
Class.forName("com.mysql.jdbc.Driver"); // JAR not in classpath!
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
// Old (MySQL 5.x)
Class.forName("com.mysql.jdbc.Driver");

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

Scenario 2: Gradle Dependency Missing

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

Scenario 3: Classpath Not Set Correctly

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

Scenario 4: Handle Gracefully

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

Key Takeaways
  • ClassNotFoundException occurs when JVM cannot find a class in the classpath at runtime
  • Most commonly caused by missing JAR files or incorrect dependency configuration
  • Use Maven or Gradle to manage dependencies and classpath automatically
  • Class names are case-sensitive and must be fully qualified
  • Library updates can change class names — check migration guides
  • Always catch ClassNotFoundException as it is a checked exception

Frequently Asked Questions


Ready to Level Up Your Skills?

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