3 ways to solve Eclipse - main class not found error

Like many Java programmers who gets "Error: Could not find or load main class Main" while running 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 Java program, I get an annoying popup complaining about "could not find or load the main class, the program will exit". I 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, program will exit error".

I spent good two hours trying everything I could, calling teammates for help (getting an extra pair of eye 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 choosing "Debug As Java program" and found that it was throwing ClassNotFoundEexception for of a third-party class.

It was a kind of break-through 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 existing run configuration where User entry was not pointing to 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 a trouble for me and taken a lot of time. I don't want to face the same situation again, especially if I am in urgency 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 "could not find or load main class error" in Eclipse. 

1) Delete the existing run configuration
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.

"Error: Could not find or load main class Main"  solution in Eclipse

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 default classpath and added new project without 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 main class not found, similar to the annoying popup below:

How to fix main class not found error in Eclipse

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 Java program and then edit the run configuration. This solved the problem in my case. You can further read Core Java Volume 1 - Fundamentals 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 build path is either deleted or moved to other 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 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.

Eclipse Error: Could not find or load main class solution

To  solve this problem just follow below steps:
  • Go to configure build path
  • Check which JAR is missing
  • Remove them and add them from the new location. 
You can further read Eclipse in Action: A Guide for the Java Developer to learn more about setting and building Java projects in Eclipse.

3) Debug the program
This will tell you exactly which class is missing in the classpath. I mean if your main class is referring 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 look 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 console.

How to solve main class not found error in Eclipse

There were a couple of more reasons of "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 future.

Other Eclipse tutorials and tips for Java developers
  • How to connect Eclipse to Oracle database? (see here)
  • How to make executable JAR file in Eclipse? (see here)
  • How to increase heap size of 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 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)

The eclipse official page 

No comments:

Post a Comment