Eclipse “Error: Could not find or load main class”

2019-01-02 17:39发布

I have a project in eclipse on my laptop that I pushed to Git https://github.com/chrisbramm/LastFM-History-Graph.git

It works fully on my laptop and runs/builds without a problem but on my desktop it doesn't Eclipse gives the error

Error: Could not find or load the main class lastfmhistoryguis.InputPanel

I've tried building the project from:

Project>Build Project

But nothing happened. I've set the PATH variables on this computer to JRE6, JRE7 and JDK 1.7.0 even though these aren't set on my laptop.

I did have Jar file (last.fm-bindings-0.1.1.jar) that was in my .classpath file that was in C:\Users\Chris\Downloads folder on my laptop hence it wasn't included in the git tree which I recently brought into the project folder and committed ,but I'm not sure whether I have done it right. Would this also be causing a problem but there isn't a main argument in there.

I can't work out now, what I need to check/change.

30条回答
梦该遗忘
2楼-- · 2019-01-02 18:32

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.

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.

查看更多
明月照影归
3楼-- · 2019-01-02 18:32

I had this error. It was because I had static void main(String[] args)
instead of public static void main(String[] args)

I spent nearly an hour trying to figure that out.

Note: The only difference is that I didn't declare main to be public

查看更多
ら面具成の殇う
4楼-- · 2019-01-02 18:33

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"/>
查看更多
心情的温度
5楼-- · 2019-01-02 18:33

Check the workspace error log (Windows-> Show View -> Error log). If you see any of the jar's imported is corrupted, remove the corresponding repository folder and re-import again.

查看更多
残风、尘缘若梦
6楼-- · 2019-01-02 18:33

Happened to me when I changed installed JREs to JDK7 instead of JRE7. Everything compiled but I couldn't run anything from Eclipse "eclipse-error-could-not-find-or-load-main-class"

Fix; going back to previous JRE7.

查看更多
梦醉为红颜
7楼-- · 2019-01-02 18:34

If You are using eclipse then the following steps will solve your problem:

Go to Run -> Run Configurations -> Main Class Search -> Locate your class manually -> Apply -> Run

查看更多
登录 后发表回答