ClassNotFoundException is a checked exception that occurs when the Java Virtual Machine (JVM) or Class Loader cannot locate a class requested during runtime.
Unlike compilation errors, this exception occurs after the application has started when Java attempts to dynamically load a class.
For example:
Class.forName("com.mysql.cj.jdbc.Driver");
If the MySQL JDBC driver is missing from the classpath, Java throws:
Exception in thread “main”
java.lang.ClassNotFoundException:
com.mysql.cj.jdbc.Driver
When Does ClassNotFoundException Occur?
This exception commonly occurs in situations such as:
- Missing JAR files
- Incorrect classpath configuration
- Missing JDBC driver
- Incorrect package names
- Wrong class names
- Build configuration issues
- Missing dependencies in Maven or Gradle
- Incorrect deployment of applications
Difference Between ClassNotFoundException and NoClassDefFoundError
Many beginners confuse these two errors.
| ClassNotFoundException | NoClassDefFoundError |
|---|---|
| Checked exception | Error |
| Occurs during runtime when Java cannot load a class dynamically | Class existed during compilation but is unavailable at runtime |
| Usually caused by missing classpath entries | Usually caused by missing .class files or failed initialization |
| Can be handled using try-catch | Generally indicates a deployment or configuration issue |
Solution 1: Verify the Class Name
One of the simplest causes is an incorrect class name.
Incorrect:
Class.forName("com.mysql.jdbc.Driver");
For newer versions of MySQL Connector/J, use:
Class.forName("com.mysql.cj.jdbc.Driver");
Always verify the class name from the library documentation
Solution 2: Add the Required JAR File
If the required class belongs to an external library, ensure its JAR file is included in your project.
Example:
mysql-connector-j-9.x.x.jar
Without the required library, the JVM cannot locate the class.
Solution 3: Check the Classpath
The classpath tells Java where to search for compiled classes and external libraries.
Display the classpath.
Windows
echo %CLASSPATH%
Linux/macOS
echo $CLASSPATH
If your required JAR or compiled classes are missing from the classpath, Java throws ClassNotFoundException.
Run with a custom classpath:
java -cp “.;lib/*” Main
For Linux/macOS:
java -cp “.:lib/*” Main
Solution 4: Verify Maven Dependencies
If you’re using Maven, ensure the dependency exists in your pom.xml.
Example:
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version>
</dependency>
After adding the dependency:
- Reload Maven
- Clean the project
- Rebuild the application
Solution 5: Verify Gradle Dependencies
For Gradle projects:
implementation ‘com.mysql:mysql-connector-j:9.0.0’
Then run:
gradle build
or
./gradlew build
Ensure dependencies are downloaded successfully.
Solution 6: Verify Package Names
Suppose your class is:
package com.demo;
Then loading it should be:
Class.forName("com.demo.Employee");
Incorrect:
Class.forName("Employee");
The package name must exactly match the class location.
Solution 7: Check the Build Output
Sometimes the source code exists, but the compiled .class file is missing.
Rebuild the project.
Command line:
javac Employee.java
For Maven:
mvn clean install
For Gradle:
gradle clean build
This regenerates all compiled classes.
Solution 8: Verify IDE Configuration
If you’re using IntelliJ IDEA, Eclipse, or VS Code:
Check:
- Project SDK
- Build Path
- Module Dependencies
- External Libraries
- Run Configuration
Sometimes simply selecting Build → Rebuild Project resolves the issue.
Solution 9: Verify Deployment
For web applications, ensure all libraries are packaged correctly.
For example:
WEB-INF/
lib/
should contain all required JAR files.
If the JAR is missing after deployment, the application server cannot load the class.
Solution 10: Check Dynamic Class Loading
Many frameworks use reflection.
Example:
Class clazz = Class.forName("com.demo.Employee");
Verify:
- Correct package
- Correct class name
- Class exists in the application
Even a small spelling mistake causes ClassNotFoundException.
Solution 11: Verify Java Modules (Java 9+)
When using the Java Module System, ensure the required module is available.
Example:
module myapp {
requires java.sql;
}
Missing required modules can prevent class loading.
Solution 12: Clean and Rebuild the Project
Old build artifacts may cause class loading issues.
Delete:
target/
or
build/
Then rebuild the project.
For Maven:
mvn clean package
For Gradle:
gradle clean build
This resolves many dependency-related issues.
Real-World Example
Suppose you have:
public class Database {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded");
}
}
If the MySQL Connector JAR is missing, you’ll get:
Exception in thread “main”
java.lang.ClassNotFoundException:
com.mysql.cj.jdbc.Driver
After adding the JDBC driver to the project, the output becomes:
Driver Loaded
Common Scenarios That Cause ClassNotFoundException
| Scenario | Solution |
|---|---|
| Missing JAR file | Add the required library |
| Wrong class name | Verify package and class names |
| Incorrect classpath | Update the classpath |
| Missing Maven dependency | Add dependency to pom.xml |
| Missing Gradle dependency | Add dependency to build.gradle |
| Incorrect deployment | Include all required libraries |
| Package mismatch | Use the fully qualified class name</td |
| Build failure | Clean and rebuild the project |
| Reflection with incorrect class name | Verify Class.forName() argument |
| IDE configuration issue | Clean and rebuild the project |
Best Practices to Prevent ClassNotFoundException
Follow these recommendations:
- Keep all required libraries in your project.
- Use dependency management tools like Maven or Gradle.
- Avoid hardcoding class names unless necessary.
- Verify package names before using reflection.
- Clean and rebuild the project after dependency changes.
- Use the latest compatible versions of external libraries.
- Keep your IDE project configuration synchronized with your build tool.
- Test application deployment before releasing it to production.
Frequently Asked Questions (FAQs)
Is ClassNotFoundException a checked or unchecked exception?
ClassNotFoundException is a checked exception because it extends the Exception class. It must be caught or declared using the throws keyword.
Why does ClassNotFoundException occur?
It occurs when the JVM cannot locate a required class during runtime. Common reasons include missing JAR files, incorrect classpaths, wrong package names, or missing dependencies.
How do I fix ClassNotFoundException?
You can fix it by:
- Adding missing JAR files.
- Verifying the classpath.
- Checking package and class names.
- Rebuilding the project.
- Ensuring Maven or Gradle dependencies are correctly configured.
- Verifying deployment includes all required libraries.
Is Class.forName() always required in modern Java?
Not always. For many modern JDBC drivers, explicit calls to Class.forName() are no longer necessary because the driver is automatically discovered through the Service Provider mechanism. However, some legacy applications and frameworks may still use it.
Conclusion
ClassNotFoundException is a common Java runtime exception that indicates the JVM cannot locate a required class while the application is running. The most frequent causes include missing JAR files, incorrect classpaths, invalid package names, and dependency configuration issues. By understanding how the Java Class Loader works, managing dependencies properly with Maven or Gradle, maintaining a correct project structure, and rebuilding your application after configuration changes, you can quickly resolve this exception and prevent it from occurring in future projects.