W3docs

Error: Could not find or load main class in intelliJ IDE

If you receive the error "Could not find or load main class" in IntelliJ IDEA, it means that the Java Virtual Machine (JVM) cannot find the class with the main method that you are trying to run.

If you receive the error "Could not find or load main class" in IntelliJ IDEA, it means that the Java Virtual Machine (JVM) cannot find the class with the main method that you are trying to run.

There are several possible reasons for this error:

  1. The class name is spelled incorrectly: Make sure that you have spelled the class name correctly in the "Run/Debug Configurations" dialog.
  2. The class is not in the correct package: Make sure that the class is in the correct package. If the class is in a package other than the default package, you need to specify the fully qualified class name (including the package name) in the "Run/Debug Configurations" dialog.
  3. The classpath is not set correctly: Make sure that the classpath is set correctly in the "Run/Debug Configurations" dialog. The classpath should include all the directories and jars that contain the classes that you are trying to run.
  4. There are multiple classes with the same name: If you have multiple classes with the same name, you need to specify the fully qualified class name (including the package name) in the "Run/Debug Configurations" dialog.

Step-by-Step Fix in IntelliJ IDEA

  1. Open Run > Edit Configurations... (or click the dropdown next to the Run button).
  2. Select your application configuration from the left panel.
  3. In the Main class field, click the folder icon to open the class picker.
  4. Search for your class, ensure it contains a public static void main(String[] args) method, and click OK.
  5. Verify the Working directory and Module fields are correct.
  6. Click Apply and OK, then run the configuration again.

Code Example

Ensure your class follows this structure:

package com.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

The fully qualified class name is com.example.Main. In IntelliJ, you can copy this by right-clicking the class file and selecting Copy Reference > Fully Qualified Name.

IntelliJ-Specific Checks

  • Module SDK: Go to File > Project Structure > Modules. Ensure the correct JDK is selected under the SDK tab.
  • Compiler Output Path: In the same Modules settings, check the Paths tab. Ensure the Compiler output points to a valid directory (e.g., out/production/<module-name>). If it's empty or incorrect, IntelliJ won't compile classes to the expected location.
  • Classpath/JARs: For external libraries, go to File > Project Structure > Modules > Dependencies. Click + > JARs or directories to add required .jar files. IntelliJ usually manages the classpath automatically, but manual JARs must be explicitly added here.

To fix the "Could not find or load main class" error, you need to identify the cause of the problem and take the appropriate action. You may need to check the spelling of the class name, make sure that the class is in the correct package, set the classpath correctly, or specify the fully qualified class name if you have multiple classes with the same name.