Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse "Error: Could not find or load main class"

If you create a java class with public static void main(String[] args), Eclipse will run that main method for you by right clicking on the file itself, or on the file in the project explorer, then choosing:

"Run As" -> "Java Application."

Once you do this, Eclipse stores information about your class, so you can easily run the class again from the Run As menu (Green Play Button on the toolbar) or from the Run Configurations dialog.

If you subsequently MOVE the java class (manually, or however), then again choose

"Run As" -> "Java Application,"

from the new location, Eclipse will run the original stored configuration, attempt to invoke this class from its original location, which causes this error.


SOLUTION:
For me, the fix was to go to the run configurations, (Green Play Button -> Run Configurations) and remove all references to the class. The next time you run

"Run As" -> "Java Application"

Eclipse will write a new configuration for the moved class, and the error will go away.


tl;dr: Clean your entire Build Path and everything you ever added to it manually. This includes additional sources, Projects, Libraries.

  • Project -> Clean
  • Make sure Project -> Build automatically is active
  • Project -> Properties -> Java Build Path -> Libraries: Remove any external libs you have ever added. Don't remove standard libraries like the JRE System Library.
  • Try to run your main class now. The "class could not be found / load" error should be gone. Try adding your external libs/jars one after each other.

Reason behind this: The compiler had issues linking the libraries to the project. It failed and produced a wrong error message.

In my case, it should have been something like "Could not add AutoHotkey.dll to the build path" because that was what made the compiler fail.


If this is still not working, have a look at the built-in ErrorLog of Eclipse:

Window -> Show View -> General -> Error Log


In your classpath you're using an absolute path but you've moved the project onto a new machine with quite possibly a different file structure.

In your classpath you should therefore (and probably in general if you're gonna bundle JARS with your project), use relative pathing:

In your .classpath change

<classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/><classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/>

to

<classpathentry kind="lib" path="last.fm-bindings-0.1.1.jar"/>

I did all the things mentioned above, but none of them worked for me

My problem resolved as follows:

  1. Right click on your Project > Properties > JavaBuildPath > Libraries.
  2. Remove the jar file, having a red flag on it.
  3. If problem persists try the solution below. This worked for me when I faced this problem second time:
    1. Right-Click Project > Properties > Java Build Path > Libraries
    2. Remove Library
    3. Add Library. (Choose the JRE System Library )