Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse can't find / load main class

Tags:

java

eclipse

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?

like image 390
aretai Avatar asked Apr 27 '12 19:04

aretai


People also ask

Why could not find or load main class?

Reasons to Occur Error The error generates because the JVM fails to load the main class or package name. There are some other reasons that generate the same error, as follows: The class has been declared in the wrong package. Dependencies missing from the CLASSPATH.

How do I select main class in eclipse?

In the Main tab, make sure that the "Main class:" is set to quickstart. Main . Eclipse should have identified this for you; if not, click Search... and select the class.


2 Answers

.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

like image 198
Shehaaz Avatar answered Sep 28 '22 03:09

Shehaaz


  • 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...
like image 45
Paul Vargas Avatar answered Sep 28 '22 05:09

Paul Vargas