Eclipse error “Could not find or load main class”

后端 未结 26 1476
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 01:31

I know there are many duplicates of this question, but I have looked at them all, and none of them have solved the issue.

I am trying to run a class that has a main

26条回答
  •  执念已碎
    2020-12-07 01:42

    I had the same problem. In my case I did the following, to resolve it.

    1. Select the project in the Eclipse IDE, right click, and open properties dialog.
    2. Select "Java Build Path" item on the left panel of the Properties dialog.
    3. Select the "Libraries" tab, on the right hand panel.
    4. Remove the libraries (jars), which are marked red, or fix the path.
    5. Save the Properties dialog. After removing the "Not found" libraries, Eclipse run/debug started working. The "Not found" message would also show up in the "Problems" pane. which you can open using, Window->Show View->Problems, command sequence. This pane gives you useful messages. The same message might show up for many reasons, other than what I encountered.

    Observation: The classes directory in my case was not created, which means that the javac failed. On command line also, javac works the same way, if you have multiple java files in the same invocation of javac, and one java file encounters a problem, then no class files are created.

    Which means Eclipse puts all the java files in the project with single invocation of javac.

    Now if there are no class files generated, obviously, the message now makes sense. The compile errors are also shown in the "Problems" pane.

    Now this would be the case if there is a compile error. If just a jar file which is not used at all, is on the classpath, there should not be any problem.

    Even if my project had problems, I ignored it, because I was just testing a single class, and was thinking that only that file would get compiled and run, because it was a standalone test, without any dependencies.

    But that is not the case with Eclipse. I wish they did it that way. As you can see lot of people got burnt this way.

    Thanks

提交回复
热议问题