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

Whenever we try to load a class, if the class loader is not able to find the class at the specified path a ClassNotFoundException is generated.
This may occur while executing java program, loading a class explicitly using forName() method of the class named Class or the loadClass() method of the ClassLoader class. These two classes accept string values representing the class names and loads 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 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.
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.........");
   }
}
D:\>javac -d . Sample.java
This will create a package in the current directory myPackage.example and generates the .class file of the Sample class in it. Therefore, while loading this class, you need to have your it in the same directory and pass the absolute class name to Class.forName() or, loadClass()
 Live Demo
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 spelt 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)
If you are trying to access a particular class from another directory you need to set the class path for −
The folder (outer most package) containing the .class file.or,
The folder (outer most package) containing the .class file.
or,
The jar file containing the class.
The jar file containing the class.
Suppose, we have rectified the spelling issue and trying to load the class from a Java file which is in the directory E://
 Live Demo
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 the 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 set class path 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.........