Error Could Not Find or Load Main Class Eclipse Solution
Like many Java programmers who get "Error: Could not find or load main class Main" while running the Java program in Eclipse, I was also getting the same problem recently. The "Error: Could not find or load main class" was rendered me puzzled even after my 10+ years of experience with Java errors and exceptions. Whenever I run my Java application either by Run configurations or right-click and run as a Java program, I get an annoying popup complaining about "could not find or load the main class, the program will exit".
I further checked, the project was compiling fine, I can see the .class file for the main class in the bin directory of Eclipse, and I can even see the project and output folder added on the classpath of Run configuration, but still, I am getting the "could not find main class, the program will exit error".
I spent good two hours trying everything I could, calling teammates for help (getting an extra pair of eyes always helps in this kind of situation), searching on the internet, and lots of trial and error, but the error was persistent.
The first clue I got when I debug my Java program. I debug the Java application in Eclipse by right click and choose "Debug As Java program" and found that it was throwing ClassNotFoundEexception for a third-party class.
It was a kind of breakthrough after wasting such a long time but still, the clue wasn't enough because the class belongs to one of the dependent JAR which was available in classpath but somehow Eclipse was not seeing it.
My error was resolved after deleting the existing run configuration where the User entry was not pointing to the default classpath. Just deleting the run configuration and re-creating it by running the class by right click, "Run As Java program" will fix the error.
Solution of Error: Could not find or load main class in Eclipse
Even though I managed to solve the problem but it was too much trouble for me and taken a lot of time. I don't want to face the same situation again, especially if I am urgent to run my Java program to debug an urgent production issue. Hence, I have decided to jot down my experience with a couple of more tricks from the past. In this article, I'll share three useful tips to solve the "could not find or load main class error" in Eclipse.
1. Delete existing run configurations
When you run the main class as "Run as Java Program", it adds the default classpath of the project as User entries into Run Configurations's classpath as shown below.
The problem in my case was that my project also needed native libraries and due to other errors, I have been fiddling with classpath. It seems while creating the run configuration by copying from another project, I deleted the old project with the default classpath and added a new project without the default classpath.
This caused the problem as Eclipse was not detecting any dependent JAR in the project. The problem was obscured by the error because it always complains about the main class not found, similar to the annoying popup below:
Remember, if your main class is dependent upon any other class or JAR file and if you believe everything in the classpath, then just delete the Run configurations and Run the main class as a Java program and then edit the run configuration. This solved the problem in my case. You can further check out these free Java Programming Courses to learn more about classpath and how JVM search and load class files.
Also, some of you might not be getting this popup because I got the popup only on Eclipse Kepler but when I tried to reproduce the issue in "Eclipse IDE for Java developers" on Eclipse Luna, I just got the "Error: Could not find or load main class Main" in red and bold.
2. Check for missing library
If you see a red exclamation mark in your project means some of the libraries which are added in the build path are either deleted or moved to another location. Until you fix this issue, your project will not compile and no class is created in the bin directory, hence when you run your Java program, Eclipse's classloader will not find the main class and throw this error.
Here is how your project will look like if any library from the build path is missing due to any reason e.g. somebody copied to another directory or you close the project from which they were added.
To solve this problem just follow the below steps:
- Go to configure build path
- Check which JAR is missing
- Remove them and add them to the new location.
You can further join these Eclipse online courses to learn more about setting and building Java projects in Eclipse.
3. Debug the Java program
This will tell you exactly which class is missing in the class path. I mean if your main class is referring to another class from a third-party library and that library is not in the classpath, Eclipse will throw the same "main class not found error". When you run the program in debug mode, you can see exactly which class the program is not able to found.
Here is how the debug configuration looks like, you just need to click the debug to start debugging your program, the JVM will start automatically if it's not able to find the main class and print the actual class and error in the console.
There were a couple of more reasons for "Error: Could not find or load main class Main" in Eclipse. I'll slowly add those as well. If you want to help you can put your problem and solution related to Eclipse main class not found error and I'll add those reasons in the main posts. I intend to make this post my go-to place whenever I get the ClassNotFoundException or NoClassDefFoundError in Eclipse to avoid wasting more time in the future.
Other Eclipse tutorials and tips for Java developers
- How to connect Eclipse to the Oracle database? (see here)
- How to make an executable JAR file in Eclipse? (see here)
- How to increase the heap size of the Java program running in Eclipse? (see here)
- How to fix Unsupported major.minor version 52.0 error in Eclipse? (see here)
- 3 Maven Eclipse tips for Java developers (see here)
- Top 30 Eclipse keyboard shortcuts for Java programmers (see here)
- How to setup remote debugging in Eclipse (see here)
- How to attach source code for the JAR file in Eclipse for debugging? (see here)
- How to fix maven dependency search not working in Eclipse? (solution)
- 3 Books to learn and master Eclipse IDE for Java programming (books)
Error Could Not Find or Load Main Class Eclipse Solution
Source: https://www.java67.com/2016/09/3-ways-to-solve-eclipse-main-class-not-found-error-java.html