Tuesday, May 12, 2026

Resolving ClassNotFoundException in Java – Causes, Examples, and Fixes

In this article we’ll see what is java.lang.ClassNotFoundException and how to resolve ClassNotFoundException in Java.

ClassNotFoundException in Java

java.lang.ClassNotFoundException is a child class of Exception class. Being a descendant of Exception class makes it a checked exception that means it needs to be either caught in a method (with in a try-catch block), or the method needs to specify that exception in a throws clause declaration.

When is ClassNotFoundException thrown

ClassNotFoundException is thrown when the Java ClassLoader tries to load a class through its string name but the class definition cannot be found in the classpath.

Methods that can be used for loading a class through its String name are as following-

  • The Class.forName(String className) method in class Class.
  • The findSystemClass(String name) method in class ClassLoader.
  • The loadClass(String name) method in class ClassLoader.

Since class loading happens at runtime, ClassNotFoundException is always thrown during execution, not at compile time.

Java ClassNotFoundException example

As the name of the exception itself suggests this exception is thrown when the class that has to be loaded is not found. One scenario where you may find this exception is if you try to load JDBC drivers using Class.forName() but the required jar is not in the classpath.

I have also encountered ClassNotFoundException when upgrading to new version of jar in my system and using some new classes available in that jar but forgetting to update the same JAR on the test or production server. With that you get the scenario which is often quoted by the developers "In my system it is working fine"!!!

For example following program tries to load Oracle driver but the required ojdbcxx.jar is not present in the classpath.

public class JDBCCon {
  public static void main(String[] args) {
    Connection connection = null;
    try {
      // Loading driver
      Class.forName("oracle.jdbc.driver.OracleDriver");

      // Creating connection
      connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
                   "root", "admin");

      // creating Statement
      Statement stmt = connection.createStatement();  

      // Executing Query
      ResultSet rs = stmt.executeQuery("Select * from Employee");

      // Processing Resultset
      while(rs.next()){
        System.out.println("id : " + rs.getInt("id") + " Name : " 
          + rs.getString("name") + " Age : " + rs.getInt("age")); 
      }
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      if(connection != null){
        //closing connection 
        try {
          connection.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
 at java.base/java.lang.Class.forName0(Native Method)
 at java.base/java.lang.Class.forName(Class.java:332)
 at org.netjs.prgrm.JDBCCon.main(JDBCCon.java:16)

As evident from the exception stack trace OracleDriver is not found so the ClassNotFoundException is thrown.

Resolving ClassNotFoundException in Java

As seen in the above example it is easy to see from the error log or exception stack trace which class is causing the exception. Then you need to check whether the required jar (where the class resides) is present in the classpath or not. So, to resolve ClassNotFoundException in Java, follow these steps-

  1. Check the Error Log- Identify the exact class name mentioned in the stack trace.
  2. Verify Classpath Configuration- Ensure the required JAR file containing the class is present in the classpath.
  3. Confirm JAR Version Compatibility- Double‑check that the class you're trying to load exists in the specific version of the JAR you're using.
  4. Package Deployment Correctly- If you’re deploying a WAR or JAR to a server, make sure all dependencies are bundled properly and there are no omissions. Missing or misplaced classes often cause this exception.
  5. Check Startup Scripts- Sometimes classpath modifications in startup scripts can override or exclude required JARs. Validate your server configuration.

That's all for this topic Resolving ClassNotFoundException in Java – Causes, Examples, and Fixes. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. java.lang.UnsupportedClassVersionError - Resolving UnsupportedClassVersionError in Java
  2. Difference Between throw And throws in Java
  3. Multi-Catch Statement in Java Exception Handling
  4. Try-With-Resources in Java With Examples
  5. Java Exception Handling Interview Questions And Answers

You may also like-

  1. intern() Method in Java String
  2. Private Methods in Java Interface
  3. Java Program to Convert a File to Byte Array
  4. Batch Processing in Java JDBC - Insert, Update Queries as a Batch
  5. ConcurrentSkipListMap in Java
  6. Spring MVC Example With @PathVariable - Creating Dynamic URL
  7. Spring Web Reactive - Spring WebFlux Example Using Annotation-Based Programming
  8. Python Installation on Windows

No comments:

Post a Comment