Trusted answers to developer questions

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

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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.

RELATED TAGS

java
exception
class
error
classnotfoundexception
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?