Error Could not find or load main class in intelliJ IDE
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Java developers often encounter the error message "Could not find or load main class" while working with IntelliJ IDEA, one of the most popular Integrated Development Environments (IDE) for Java development. This error typically occurs when the IDE is unable to locate the class with the main method to run the application. Understanding the causes and solutions to this problem can significantly streamline the development process.
Understanding the Error
The error "Could not find or load main class" suggests that the Java Virtual Machine (JVM) has failed to identify the entry point of the application, i.e., the class containing the main method. Several factors might contribute to this issue in IntelliJ IDEA:
- Incorrect Configuration: The class containing the
mainmethod is not correctly configured in the IDE's run/debug configurations. - Compilation Issues: The class files might not be generated correctly if there are compilation errors.
- Classpath Issues: The JVM might not be able to find the class files if they are not included in the classpath.
- Project Setup Issues: Incorrect project setup or corrupted project metadata can also lead to this problem.
Common Scenarios and Solutions
Misconfiguration in Run/Debug Configurations
Sometimes, the run/debug configurations might be set incorrectly or point to an outdated or non-existent main class. To fix this, you need to ensure that the correct class is specified in the configuration:
- Open the Run/Debug Configurations by going to Run > Edit Configurations.
- Check the configuration used for running your application and ensure the correct main class is specified under the 'Main class' field.
Compilation Errors
Before running the application, make sure that your project is free from compilation errors. Any error during the build process can prevent the generation of .class files, making them unavailable to the JVM. You can view and resolve compilation errors in the Messages view in IntelliJ IDEA.
Issues with Classpath
The classpath defines where the JVM looks for user-defined classes and packages. Errors might arise if the classes are not compiled into the correct directory or if the classpath is not set properly:
- Verify that the output directories (typically
outortarget) contain the correct compiled.classfiles. - Ensure the module's dependencies are correctly configured and available. This can be done in File > Project Structure > Modules and checking the dependencies tab.
Project Setup Errors
Corrupted project settings or improper project setup can also lead this error to occur. To resolve any potential project setup issues:
- Refresh the project by syncing with the build tool you're using (e.g., Maven or Gradle). This can be done via View > Tool Windows > Maven (or Gradle) > Reload project.
- Invalidate caches and restart IntelliJ IDEA to clear any corrupted cache files. This is available under File > Invalidate Caches / Restart.
Other Considerations
If the problem persists even after trying the above solutions, consider the following additional steps:
- Ensure that your project is using a compatible version of the JDK for compilation and execution.
- Re-import the project into IntelliJ IDEA, which can help reset configuration issues.
Summary Table
| Issue | Solution Suggestions |
| Configuration Error | Adjust Run/Debug settings |
| Compilation Error | Fix compilation issues |
| Classpath Error | Verify and adjust classpath settings |
| Project Setup Error | Re-sync or re-import project; Invalidate caches |
| JDK Compatibility | Check and adjust JDK settings |
By identifying the root cause of the "Could not find or load main class" error and applying the appropriate solution, you can resolve issues more efficiently and focus on developing your Java application in IntelliJ IDEA.
Related reading
- Error creating bean with name 'entityManagerFactory' defined in class path resource Invocation of init method failed
- Error executing Hello World for AWS Lambda in Java
- Error File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it
- Error IllegalStateException No target Validator set after upgrade from Spring Boot 3.1.5 to 3.2.0
- error Could not get BatchedBridge, make sure your bundle is packaged properly on start of app
- ERROR Could not install packages due to an OSError WinError 5
- Error in TiDB `java.sql.BatchUpdateExecptionstatement count 5001 exceeds the transaction limitation`
- Error inflating when extending a class

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.