[java] Eclipse can't find / load main class

My Eclipse (Indigo) was running just fine. I created a simple class Hello. It is placed in package cont in the folder ch13. However, when I try to run it from Eclipse I get info from Java Virtual Machine Launcher:

Could not find the main class: cont.Hello.  

I tried to run other classes from this package and they run just fine (that is the classes that existed there before). However any new class I create in this package has these problems. Actually any new class I create in Eclipse runs into this problems. I decided to check how it works from the command line. It seems that the problem still exist - I get same error. I checked the path and it is C:\Program Files\Java\jdk1.7.0_02\bin, which is correct (note the other classes are running from Eclipse just fine). I tried to run with java -cp . Hello and there are some Errors produced starting with java.lang.NoClassDefFoundError: Hello (wrong name: cont/Hello). Code itself is simple:

package cont;

public class Hello {
    public static void main(String[] args){
        System.out.println("Hello");
    }

}

How can I fix it so that my classes still run under Eclipse?

This question is related to java eclipse

The answer is


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.


.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


Note: This worked in the past and I received many up votes. Perhaps this is not a solution anymore - but it once was - as the eclipse version was indicated.


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 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.


I read so many blogs and tried so many tricks but my problem not resolved. I was able to run the code but not able to generate the jar file. :( Sad..

But I tried something which might be very silly but worked for me and bought eclipse on trace. What I did was.. Just deleted the main method from the class. Saved it. Did undo to bring the main class back. Tada... Issue resolved... Just one think would like to say, keep your eclipse in "Build Autometically" mode.


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[]) {
    
    }
}

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.


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();
}

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


Move your file into a subdirectory called cont


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 this issue after upgrading to the Eclipse 2019-12 release. Somehow the command line to launch the JVM got too long and I had to enable the jar-classpath option in the run configuration (right click on file -> run as -> run configs).


  • 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...

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.


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 had the same problem.I solved with following command maven:

mvn eclipse:eclipse -Dwtpversion=2.0

PS: My project is WTP plugin


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


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

Tadaaaa !! It works


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;