Why do we get ClassNotFoundException when the class exists in Java?



Whenever we try to load a class, ClassNotFoundException occurs when the application tries to load a class at runtime using methods like Class.forName() or ClassLoader.loadClass(), but the JVM cannot locate the class in the classpath.

The following are the reasons of ClassNotFoundException occurence and they are -

  • Incorrect Class Name

  • Incorrect Package Structure

  • Class is not found in the Classpath

Incorrect Class Name

This may occur while executing a Java program, loading a class explicitly using the forName() method of the class named Class or the loadClass() method of the Loader class. These two classes accept string values representing the class names and load the specified classes.

While passing class names to these methods, you need to make sure that -

  • The class names you pass to these methods should be accurate.

  • The specified class (along with the package) should be either in the current directory or, its path should be listed in the environment variable classpath.

Assume we have created a class named Sample in the directory D:// and compiled as shown below ?

package myPackage.example;
public class Sample {
   static {
      System.out.println("The class named Sample loaded successfully.........");
   }
}

Compilation

D:\>javac -d . Sample.java

This will create a package in the current directory myPackage.example and generate the .class file of the Sample class in it. Therefore, while loading this class, you need to have it in the same directory and pass the absolute class name to Class.forName() or, ClassLoader.loadClass()

Incorrect Package Structure

Even if the class exists, if its package name doesn't match the directory hierarchy where our .class file exists. We will get the ClassNotFoundException.

public class ClassNotFoundExample {
   public static void main(String args[]) {
      try {
         Class.forName("myPackage.exampl.Sample");
      }catch (ClassNotFoundException ex) {
         ex.printStackTrace();
      }
   }
}

On executing the above program, since the name of the package is wrongly spelled you will get the following exception.

D:\>java ClassNotFoundExample
java.lang.ClassNotFoundException: myPackage.exampl.Sample
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
 at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
 at java.base/java.lang.Class.forName0(Native Method)
 at java.base/java.lang.Class.forName(Unknown Source)
 at ClassNotFoundExample.main(ClassNotFoundExample.java:4)

Class not found in Classpath

If you are trying to access a particular class from another directory, you need to set the class path for the folder (outermost package) containing the .class file or the jar file containing the class.

Suppose, we have rectified the spelling issue and are trying to load the class from a Java file which is in the directory E:// -

public class ClassNotFoundExample {
   public static void main(String args[]) {
      try {
         Class.forName("myPackage.example.Sample");
      }catch (ClassNotFoundException ex) {
         ex.printStackTrace();
      }
   }
}

Still, you get the same exception since the package containing the .class file of the specified class (or jar file containing it) is neither in the current directory nor in the list of paths in the environment variable classpath.

E:\>java ClassNotFoundExample
java.lang.ClassNotFoundException: myPackage.example.Sample
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
 at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
 at java.base/java.lang.Class.forName0(Native Method)
 at java.base/java.lang.Class.forName(Unknown Source)
 at ClassNotFoundExample.main(ClassNotFoundExample.java:4)

In the current scenario, we need to set classpath to the directory containing the package holding the required class i.e., "D://", and execute the above Java program to make it work.

E:\>javac ClassNotFoundExample.java
E:\>java ClassNotFoundExample
The class named Sample loaded successfully.........
Updated on: 2025-04-24T15:30:50+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements