aretai
aretai

Reputation: 1641

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?

Upvotes: 32

Views: 147976

Answers (20)

moonchel
moonchel

Reputation: 111

You must have main function in your class. Like

public class MyDataBase {

    public static void main(String args[]) {
    
    }
}

Upvotes: 3

Anup Prakash
Anup Prakash

Reputation: 14724

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.

Upvotes: 0

Thomas Rokicki
Thomas Rokicki

Reputation: 644

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)

Upvotes: 4

user1050755
user1050755

Reputation: 11691

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

Upvotes: 0

Shehaaz
Shehaaz

Reputation: 763

.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

Upvotes: 36

J.Doe
J.Doe

Reputation: 21

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.

Upvotes: 0

user2310395
user2310395

Reputation: 307

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;

Upvotes: 2

Swapnil Jaju
Swapnil Jaju

Reputation: 472

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

Tadaaaa !! It works

Upvotes: -1

No Spam
No Spam

Reputation: 59

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

Upvotes: 1

Romejanic
Romejanic

Reputation: 427

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.

Upvotes: 0

ccoutsou
ccoutsou

Reputation: 19

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.

Upvotes: -1

Paul Vargas
Paul Vargas

Reputation: 42060

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

Upvotes: 20

Diogenes
Diogenes

Reputation: 29

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

mvn eclipse:eclipse -Dwtpversion=2.0

PS: My project is WTP plugin

Upvotes: -1

user2588871
user2588871

Reputation: 1

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.

Upvotes: -1

Tuomas
Tuomas

Reputation: 169

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

Upvotes: -1

Pelpotronic
Pelpotronic

Reputation: 560

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.

Upvotes: -1

work_in_progress
work_in_progress

Reputation: 767

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

Upvotes: 18

w359
w359

Reputation: 29

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

Upvotes: 0

djechlin
djechlin

Reputation: 60818

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.

Upvotes: -1

ControlAltDel
ControlAltDel

Reputation: 35096

Move your file into a subdirectory called cont

Upvotes: -2

Related Questions