How to Fix Exception in Thread Main Java.Lang.ClassNotFoundException in IntelliJ IDEA
-
the
java.lang.ClassNotFoundExceptionError in Java -
Causes of
java.lang.ClassNotFoundExceptionin Intellij IDEA -
Solution to
java.lang.ClassNotFoundExceptionin Intellij IDEA
Today’s tutorial will discuss potential reasons for the java.lang.ClassNotFoundException whenever a Java program’s main method is executed.
the java.lang.ClassNotFoundException Error in Java
java.lang.ClassNotFoundException is triggered if the ClassLoader cannot find the class in its system. In the JVM (Java Virtual Machine) core library, ClassLoader is used to load and locate a class.
This error is thrown by ClassLoader if it cannot load a class from the application library.
In addition, you should be aware of the checked nature of this exception and the necessity to properly handle it when calling methods that can trigger the java.lang.ClassNotFoundException in Java, whether via a try-catch block or the throws condition.
Let’s have an example to understand better which throws java.lang.ClassNotFoundException in Java on Intellij IDEA 14.0. After that, we’ll discuss its causes and solution.
In this example, we constructed a basic Hello Programers! program. It’s producing incorrect output; hence, the java.lang.ClassNotFoundException exception will be triggered.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Programers!");
}
}
Output:
Exception in thread "main" java.lang.ClassNotFoundException: Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Causes of java.lang.ClassNotFoundException in Intellij IDEA
The following are some of the factors that result in this exception:
- When we attempt to load a class by utilizing the class’s binary, we discover that it is not present in the classpath.
- If we use the
loadClass()function of theClassLoaderclass in Java. - The
java.lang.ClassNotFoundExceptionoccurs when the Java Virtual Machine attempts to load a class during runtime.
Solution to java.lang.ClassNotFoundException in Intellij IDEA
This is simply a problem with the Intellij IDEA. Therefore, please follow the steps below to fix it:
-
Launch IntelliJ IDEA first, and then simultaneously press Ctrl, Shift, Alt and s to open the
Project Settingswindow. -
On the left panel, select
modules, then expandyour_project_name, and finally go to(your_project_name) _main. -
Click on the
Sourcestab in the new window. And then click thexnext to the item at the top of the list. -
Click on
OK. -
From the list of sources, right-click on
D:\users\proj\platform-authorization\src\main. Then, clickOKto apply the changes. -
Lastly, build your project and run.
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedInRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack
