Javatpoint Logo
Javatpoint Logo

ClassNotFound Exception in Java

One particular exception that is confusing for beginners is the ClassNotFound Exception in Java. In this tutorial, we are going to get familiar with ClassNotFound exception and the fix for it.

ClassNotFound Exception

The name itself suggests that when the class is not ClassNotFound exception occurs. The ClassNotFound exception is raised when a Java Virtual Machine (JVM) tries to load a specific class, and that class is not found in the specified classpath. In other words, the classpath is broken. Note that ClassNotFound Exception is a checked exception.

Using the following ways one can load the class.

  • The findSystemClass() method of the class ClassLoader.
  • The forName() method of the class Class.
  • The loadClass() method of the class ClassLoader.

Fix For the ClassNotFoundException

One can take into consideration the following points while tackling the ClassNotFoundException.

  • Review properly review stack trace of the java.lang.ClassNotFoundException to identify that class that is not loaded at the runtime.
  • Also, review the requested class name to check whether its name is correct or not. It is also required to mention the appropriate .jar file in the classpath. For example, if we are trying to load class A and the mentioned .jar file in the classpath contains the files for Class B only, then the ClassNotFound exception is raised.
  • If the classpath has the appropriate files present, then there is a fair chance that the classpath is overridden. It will also cause the exception to be raised. Therefore, an exact classpath should be provided to the application.
  • If the exception is due to the inclusion of the third-party class, then it is required to add the appropriate .jar file in the classpath of the application.

Example - 1

In this example, we will see how ClassNotFound exception is raised and fixed. We will be using the findSystemClasas() method in this example.

FileName: ClassException1.java

Output:

Exception in thread "main" java.lang.ClassNotFoundException: com.google.common.collect.ImmutableList
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at java.base/java.lang.ClassLoader.findSystemClass(ClassLoader.java:1252)
	at SystemClass.loadSystemClass(ClassException1.java:6)
	at ClassException1.main(ClassException1.java:28)

Explanation: JVM is trying to load ImmutableList (at run time), which is not present. Hence, the ClassNotFound exception is raised. Note that the above exception is raised when we run the above program using the following command

Therefore, we need to add the appropriate .jar file in the classpath. We can do so using the following command.

We get the following output.

Example - 2

In the following example, we have used the forName() method of the class Class.

FileName: ClassException2.java

Output:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:375)
	at ClassException2.main(ClassException2.java:10)

Explanation: The issue is the same. JVM is trying to load the Driver class and is unable to do so. Therefore, we have to add the path where JVM can find the Driver.class file. The above code is run using the following command.

We need the following modification, in order to include the appropriate .jar file to run the program successfully.

We get the following output.

Example - 3

In the following example, we have used the loadClass() method.

FileName: ClassException3.java

Output:

Exception in thread "main" java.lang.ClassNotFoundException: com.google.common.collect.Constraint
	at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:718)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at SystemClass.loadSystemClass(ClassException3.java:6)
	at ClassException3.main(ClassException3.java:28)

Explanation: Same as the above example. The above runs smoothly if we include the jar file in the classpath. Readers are advised to do it on their own by taking the reference of the example - 1.

Classpath and ClassNotFoundException

A classpath contains the locations list from where the classes are loaded. These locations include jar files or directories. In the case of directories, the Java Virtual Machine follows a pattern in order to load a class.

For example, if one has put the directory as "C:/javaProj/MyClasses" in the classpath, and in the application, it is attempted to load the class com.myproj.myprogram, then JVM looks in the MyClasses directory and searches for the folder called com. Inside com, it looks for the directory called myproj, and inside myproj, it searches for the file called myprogram.class.

In the case of jar files, Java Virtual Machine will look in the jar file for that class that has to be loaded. Similar to the above example, a jar file is a collection of different directories in the zipped form. If we unzip a jar file, we'll get different directories and different class files that follow the above-mentioned pattern.

The JVM reads a classpath from start to end and looks for the class definition of the class that has to be loaded. For example, in following the classpath :

C:/javaProj/MyClasses; C:/javaProj/MyClasses/jkl.jar; C:/javaProj/MyClasses/xyz.jar

The JVM looks in the MyClasses directory first, to find the appropriate class that has to be loaded, and then in the jkl.jar and after that in the xyz.jar file.

Therefore, if the ClassNotFoundException is raised, then it simply means the required file is neither present in the .jar files nor in the MyClasses directory.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA