Eclipse can't find / load main class

有些话、适合烂在心里 提交于 2019-11-27 19:20:25
Shehaaz

.metadata is corrupted.

Steps:

Warning: Deleting .metadata will delete all your Eclipse configurations, plugins, project setups. Make a backup before you attempt this!

  1. Stop eclipse, delete .metadata in workspace and restart eclipse

  2. Import Project

  3. Run again

  • Removing the Run Configuration

    Sometimes I have a similar problems in some pre-release versions of eclipse. For fix the error, I delete the Run Configuration. You can find that in menu Run, Run Configurations...

    Then I launch the app with Alt+Shift+X, then J. If this don't work, Ctrl+F11.

  • Deleting the .metadata directory

    In another way, the configuration settings for your current workspace may are corrupted. Those settings are in the .metadata directory in your current workspace 1. In that case, there is no other choice than delete the directory:

    1. Close eclipse.
    2. Delete the .metadata directory.
    3. Start eclipse.
    4. Import the projects.
    5. Run the project again.

Notes

  1. You will see that directory with File > Switch Workspace > Other...

I have solved the issue following way:

Go to Run Configuration (Right Click on Java File->Run->Run Configuration).

Go to ClassPath Tab: Click on Advanced -> Add Folders -> Add bin directory (which has class file in it for Java source code)

Re run the code, now it will solve the issue. It worked for me

You must have main function in your class. Like

public class MyDataBase {

public static void main(String args[]) {

}
}

Problem

This can also be caused by a Java Build Path Problem.

In my case, I had a an error:

A cycle was detected in the build path of project {project}. The cycle consists of projects {x, y, z}.

This can occur when you include other projects in the build path of the project you wish to run. In fact, all the projects will fail to run with the error Could not find the main class: Example.class


Solution

Open

Windows -> Preferences -> Java-> Compiler -> Building -> Build Path Problems

Uncheck the Abort build when build path errors occur toggle

This seems like a can of worms if you end up with other build path errors I image. So use with caution.


  • Note: This only works if you have a "cycle error". This error message can be found in the "Markers" tab

I found the solution to this here


Info

  • Java 1.8.0_152
  • Eclipse Photon (June 2018)

I had this same problem in a Maven project. After creating the src/test/java folder within the project the error went away.

Renaming the main class should be enough (and easiest):
- Go to your class and set cursor to your class name;
- ALT + Shift + R and rename the class (build if not done automatically);
- You should be able to run it now;
- Rename the class to the old name if you want;

Another tip: I initialized static fields in a wrong order - surprisingly it didn't bring up a Problem (NullPointerException?), instead Eclipse complained with exactly the message OP posted. Correcting the static initialization order made the class run-able. Example:

private static ScriptEngineManager factory = null;
private static ScriptEngine engine = null;
static {
    engine = factory.getEngineByName("JavaScript");
    // factory is supposed to initialize FIRST
    factory = new ScriptEngineManager();
}

I found the way to fix this problem was to rename the project. If you give it a name with strange characters (in my case, :), it will have trouble locating your class. I don't know if this rule applies to numbers, but try renaming the project or making a new one and copying the files. Name it without any numbers or special characters.

Move your file into a subdirectory called cont

Standard troubleshooting steps for Eclipse should include deleting and re-importing the project at some point, which when I have dealt with this error has worked.

I solved my issue by doing this:

  • cut the entire main (CTRL X) out of the class (just for a few seconds),
  • save the class file (CTRL S)
  • paste the main back exactly at the same place (CTRL V)

Strangely it started working again after that.

In most cases and in my case there is just a depended Library missing. So make sure your project includes all depended Libraries (e.g. in a pom.xml if you are using maven) even if your class don't use them!

It is possible to have 2 groovy-xxx-all.jar files by excample in lib directory. which makes that an app is not running

user2588871

I had the same problem, this is my solution:

  1. I manually deleted the bin folder of the project
  2. Then I refreshed the project which recompiled the whole project and created a new bin with all .class files

I did it because when I performed Clean(project->clean) my .class files were not getting deleted. the above solution works for me hope its useful to others.

I had the same problem.I solved with following command maven:

mvn eclipse:eclipse -Dwtpversion=2.0

PS: My project is WTP plugin

If you are using a pre-defined run configuration, go to classpath and try "Restore Default Entries". This will reconfigure the classpath for that configuration.

This worked for me finally : RUN -> RUN CONFIGURATIONS -> DELETE THE RUN CONFIGURATION CLOSE ECLIPSE REOPEN ECLIPSE CREATE RUN CONFIGURATION AGAIN.

Tadaaaa !! It works

J.Doe

I solved this error by closing the project, removing it from eclipse and then importing it again.

Might be a little simpler than to redo the whole workspace setup.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!