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:18

I ran into this error today because I set up a hello world program and then cut and pasted a new program into the same file. To fix the problem of not finding hello world as the last was called I clicked Run-> Run Configurations and then under Main Class I clicked search and it found my new class name and replaced it with the correct new name in the text that I pasted. This is a newbie problem I know but it is also easy to fix. I hope this helps someone! Douglas

查看更多
浪荡孟婆
3楼-- · 2019-01-02 18:20

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

查看更多
旧人旧事旧时光
4楼-- · 2019-01-02 18:20

I run into the same problem, but in my case it was caused by missing (empty) source folder (it exists in original project, but not in GIT repository because it's empty).

After creating the missing folder everything works.

查看更多
流年柔荑漫光年
5楼-- · 2019-01-02 18:21

My Main class could not be found or loaded problem is caused by an interesting reason.

In our project, we are using Maven as build tool and my main class extends a class, which is on the class path but its scope was test, while the main class is not under the test package.

If your main class extends a class, first try to run your main class by removing extends part. If it runs, you will at least understand that the problem is not because of run configuration or eclipse but the class, your main class extends.

查看更多
闭嘴吧你
6楼-- · 2019-01-02 18:22

I had the same problem after I created new package("tables") in my project.

I went to Window -> Show View -> General -> Error Log and Ive read error:

JavaBuilder handling ImageBuilderInternalException while building: PizzaService

org.eclipse.core.internal.resources.ResourceException: Resource '/PizzaService/bin/tables' already exists.

as it turned out I had a text file in another source folder with the same name as this new package. So I've changed text file name from "Tables" to "Tabless" and I could run my project again.

Hope this helps.

查看更多
琉璃瓶的回忆
7楼-- · 2019-01-02 18:28

I found other solution in my case this problem: Eclipse->Preferences->Java->Installed JRE then press button Search. Select folder in Linux /usr then Eclipse found all JVM.

Select another JVM too current. It is solved for my case.

查看更多
登录 后发表回答