[Solved] java.lang.ClassNotFoundException with Example

In this article, we will discuss java.lang.ClassNotFoundException. This ClassNotFoundException is thrown when the classloader can not load the class from the system. ClassLoader is the class of the JVM core library that is used to load a class or find a class. ClassLoader throws this exception when it fails to load a class from application libraries. JVM uses the following methods to load class files.

1.    The forName method from Class class.
2.    The loadClass method from ClassLoader class.
3.    The findSystemClass method from the ClassLoader class.

Read Also: Difference between ClassNotFoundException and NoClassDefFoundError

1. ClassNotFoundException is a Checked Exception

ClassNotFoundException class inherited from ReflectiveOperationException class which has support of reflection functionalities. Also, ReflectiveOperationException inherited from Exception class. ClassNotFoundException is a checked exception. So we have to throw it from a method declaration or we should catch this exception.

public class ClassNotFoundException extends ReflectiveOperationException {

    private static final long serialVersionUID = 9176873029745254542L;

    private Throwable ex;

   
    public ClassNotFoundException() {
        super((Throwable)null);  // Disallow initCause
    }
    public ClassNotFoundException(String s) {
        super(s, null);  //  Disallow initCause
    }

    public ClassNotFoundException(String s, Throwable ex) {
        super(s, null);  //  Disallow initCause
        this.ex = ex;
    }
    public Throwable getException() {
        return ex;
    }

    public Throwable getCause() {
        return ex;
    }
}


2. Example of the ClassNotFoundException

If we execute the following program we will get ClassNotFoundException.

public class Test {
   public static void main(String[] args) throws ClassNotFoundException {
      final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
      Class.forName(ORACLE_DRIVER);
   }
}

Though we have not yet included the oracle JDBC driver library to our project it will generate following exception

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)

We should include the oracle JDBC driver library/jar file to our project to get rid of this kind of exception.

3. How to Deal with ClassNotFoundException

In this part, we should create a new class Employee.java like below code snapshot.

public class Employee {
    int id; 
    String name

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name; 
    }
}

Now in the main method of Test class, we will write below code

public class Test {
   public static void main(String[] args) throws ClassNotFoundException {
      //final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
      //Class.forName(ORACLE_DRIVER);

      Employee employee = new Employee(2,"JavaHungry");
      System.out.println(employee.getName());
   }
}

If we compile and execute the above Test class it should work.

We know that when we compile the program it will generate .class files to the output directory. For now, we will delete the Employee.class file from the output directory. And then again execute the Test.class file. Now it should generate the following output.

Exception in thread "main" java.lang.NoClassDefFoundError: Employee
     at Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: Employee
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

Here we can see that it is generating NoClassDefFoundError Exception but the root cause is ClassNotFoundException.To fix this issue we have to again compile the project. Then it should generate proper class files to the output directory. Now, the problem should be solved.

4. NoClassDefFoundError, ClassNotFoundException and UnSupportedClassVersionError

NoClassDefFoundError – When the java file is present during compile time but it's not available during runtime then this exception should occur.

ClassNotFoundException – When the classloader doesn’t find the class with the provided name, this exception should be thrown.

UnSupportedClassVersionError – When we compiled java files to class files with a java version but during runtime, if we changed the version of java, this exception should occur. When we encounter this error we should check the JAVA_HOME property in the path.

5. Conclusion

java.lang.ClassNotFoundException exception happens when the provided class file is not available in the compiled directory or the jar file of some library missing from the library folder.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry