How to Fix Exception in Thread Main Java.Lang.ClassNotFoundException in IntelliJ IDEA

Muhammad Zeeshan Feb 02, 2024
  1. the java.lang.ClassNotFoundException Error in Java
  2. Causes of java.lang.ClassNotFoundException in Intellij IDEA
  3. Solution to java.lang.ClassNotFoundException in Intellij IDEA
How to Fix Exception in Thread Main Java.Lang.ClassNotFoundException in 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:

  1. When we attempt to load a class by utilizing the class’s binary, we discover that it is not present in the classpath.
  2. If we use the loadClass() function of the ClassLoader class in Java.
  3. The java.lang.ClassNotFoundException occurs 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 Settings window.
  • On the left panel, select modules, then expand your_project_name, and finally go to (your_project_name) _main.
  • Click on the Sources tab in the new window. And then click the x next 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, click OK to apply the changes.
  • Lastly, build your project and run.
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

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.

LinkedIn

Related Article - Java Error