How to resolve the "java.lang.ClassNotFoundException" in Java

The ClassNotFoundException is thrown to indicate that the specified class cannot be found in the classpath. The following piece of code tries to load the class using forName(), but the class name can​not be found. Thus, the​ exception is thrown.

package main.java;
class MAIN {
private String TEMP = "Educative";
public static void main(String[] args) {
Class loadClass = Class.forName(TEMP);
}
}
svg viewer

Solution

Verify the specified class name and check whether or not the .jar file exists in the classpath. If it already exists, then make sure that it is not overridden in our program. If the file does not​ exist, then simply add the .jar file in the classpath.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved