Lỗi error could not find or load main class eclipse

If you are seeing an error message that says "Error: Could not find or load main class", it is likely that there is a problem with the classpath for your project.

There are a few things you can try to fix this error:

  1. Make sure that you have a public static void main[String[] args] method in your main class. This method is the entry point for your program, and is necessary for it to run.
  2. Make sure that the main class you are trying to run is in a package. If it is, then the package statement at the top of the file should match the directory structure of your project. For example, if your main class is in a package called com.example, then the file should start with package com.example;.
  3. Make sure that the classpath is set correctly. The classpath is a list of directories that the Java runtime looks in to find class files. If the classpath is not set correctly, the Java runtime will not be able to find your main class.
  4. If you are using an external library or dependency, make sure that it is included in the classpath.
  5. If you have recently added or changed any files in your project, try cleaning and rebuilding the project. This can sometimes resolve issues with classpaths and dependencies.

I hope this helps! If you are still having trouble, please provide more information about your project, such as your main class, the directory structure, and any external libraries you are using.

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 like 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.

Chủ Đề