31

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?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
aretai
  • 1,619
  • 6
  • 19
  • 30
  • Did eclipse generate the .class file for Hello? I think it's usually in bin/ or target/ if you use maven – jeff Apr 27 '12 at 20:12
  • No maven was used. Tried to find class in the cont folder but there was none. Even if compiled from command line (class is in folder then) it doesn't start – aretai Apr 27 '12 at 20:16
  • 2
    There could be many things wrong. I would check the source folder is on correct build path (right click on your project->properties->java build path->source and make sure that the path until src folder is selected), also check the output folder path in the same way.. – htulsiani Apr 27 '12 at 21:53
  • It seems that you were right htulsiani. Please make a question as such and I will accept it as correct answer. thank you – aretai May 03 '12 at 21:46
  • 1
    I had the same issue and found a much better solution on this thread (second answer): http://stackoverflow.com/questions/11239086/java-lang-unsupportedclassversionerror-unsupported-major-minor-version-51-0 It is a better solution as you don't have to delete the .metadata and waste precious time reimporting projects and ui settings – jule64 Aug 23 '13 at 16:02

20 Answers20

35

.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

Cecilya
  • 519
  • 1
  • 5
  • 20
Shehaaz
  • 753
  • 1
  • 7
  • 12
  • 1
    Is it risky to delete all of .metadata? Does this include anything that's hard to recreate or reinstall? – djechlin Feb 01 '13 at 18:03
  • the .metadata is basically a file that knows what is on your workspace in eclipse. If you are scare make a copy and you can bring it back if things blowup. – Shehaaz Feb 04 '13 at 19:06
  • 13
    I deleted workspace/.metadata (which contained other files) and I lost all plugins and perpectives. It's not a good thing to do. Obviously, after restart, the problem of missing main persisted. – HAL9000 Dec 13 '13 at 23:49
  • 1
    It solved my problem.If you don`t want to delete .metadata than copy it in another folder. – Sagar007 Feb 05 '15 at 09:45
  • 4
    please put a warning in an answer about the consequences of deleting this folder. – Eastern Monk Apr 08 '15 at 01:31
  • 6
    This is the worst answer ever. It deletes all your user libraries. DO NOT ATTEMPT! – David Brossard Aug 29 '16 at 17:12
20
  • 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...
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Delete the `.metadata` directory may help you if this is corrupted. – Paul Vargas Apr 27 '12 at 21:00
  • 2
    1.- Close eclipse 2.- In the folder-eclipse/configuration/.settings edit (with notepad) the file org.eclipse.ui.ide.prefs. In the field RECENT_WORKSPACES delete the path (ends with \n) Save the file 3.- Start eclipse – Paul Vargas Apr 27 '12 at 21:50
  • Yes, going to Run->Run Configurations and deleting the one that was causing problems fixed this for me. – Tastybrownies Aug 28 '13 at 19:07
  • 1
    I've cut pasted metadata folder to desktop. The problem is solved. then I've moved plug-ins folder back into the newly created metadata folder. As far as i can tell the problem is still solved and my plug-ins and preferences and projects are back. – WVrock Dec 07 '14 at 08:32
  • Where is the metadata directory? I cannot find it anywhere. – JohnMerlino Jan 14 '15 at 04:28
  • deleting metadata directory is not a good idea. It will erase all of the project setting, including font, indentation and other. – Ammad Feb 27 '16 at 00:08
18

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

work_in_progress
  • 747
  • 1
  • 10
  • 27
  • 4
    After the various and usual Eclipse "thrash in the dark" exercises, this solution worked for me. – J Slick Feb 27 '14 at 21:05
  • I agree. Much safer than deleting all metadata and it cured the issue correctly, namely, by adding a path reference that should have been recognized automatically but was not. – Geoffrey Malafsky Oct 14 '14 at 00:19
  • Its a maven project. So bin folder in the eclipse. – Tanu Garg Sep 03 '16 at 15:31
  • @JSlick - For Maven project which folder I need to add? src or s/main or src/main/java or src/main/java... till your java class having main method? – nanosoft Dec 23 '16 at 06:08
4

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)
Community
  • 1
  • 1
Thomas Rokicki
  • 572
  • 1
  • 5
  • 16
2

You must have main function in your class. Like

public class MyDataBase {

    public static void main(String args[]) {
    
    }
}
svarog
  • 9,477
  • 4
  • 61
  • 77
moonchel
  • 101
  • 5
2

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;

user2310395
  • 297
  • 3
  • 6
1

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

No Spam
  • 59
  • 2
  • 5
0

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();
}
w359
  • 29
  • 3
0

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.

Romejanic
  • 419
  • 1
  • 4
  • 9
0

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
J.Doe
  • 21
  • 3
0

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

user1050755
  • 11,218
  • 4
  • 45
  • 56
0

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.

Anup Prakash
  • 14,406
  • 4
  • 17
  • 12
-1

Move your file into a subdirectory called cont

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • and run it from the top directory with `java -cp . cont.Hello` – nos Apr 27 '12 at 19:44
  • The file is already in the directory called cont - shall I run it from the directory above it? I have done that and it works. However it is not the main problem. The thing is that I want the classes to run from Eclipse level, which they don't at the moment. – aretai Apr 27 '12 at 19:47
  • Make sure your eclipse debug profile for this application is correctly configured – ControlAltDel Apr 27 '12 at 19:48
  • I am using the same debug profile for all of the classes. No switching perspective or anything. Still some of them (older ones) run OK, and new ones don't – aretai Apr 27 '12 at 20:17
  • because you have moved them to a new directory. You need to update your debug profile – ControlAltDel Apr 27 '12 at 20:19
  • You really should be able to google for an answer to such a question: http://www.ibm.com/developerworks/library/os-ecbug/ – ControlAltDel Apr 30 '12 at 14:57
-1

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.

djechlin
  • 59,258
  • 35
  • 162
  • 290
-1

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.

Pelpotronic
  • 550
  • 6
  • 11
-1

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

Tuomas
  • 169
  • 2
  • 4
-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.

tjati
  • 5,761
  • 4
  • 41
  • 56
-1

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

mvn eclipse:eclipse -Dwtpversion=2.0

PS: My project is WTP plugin

Diogenes
  • 19
  • 7
-1

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.

ccoutsou
  • 19
  • 3
-1

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

Tadaaaa !! It works

Swapnil Jaju
  • 432
  • 1
  • 6
  • 14