79

Have Googled extensively on this error, but I can't seem to fix the problem. I've written a basic java program in Eclipse Juno, as follows:

public class HelloWorld {

    /**
     * @param args
     */

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

}

After clicking Run, I get: "Error: Could not find or load main class HelloWorld". This error message applies to all my other projects in the same workspace. I've tried switching workspaces but the error still appears.

I'm on Windows 7 64-bit. Any help would be appreciated!

Fezzo
  • 977
  • 1
  • 7
  • 5
  • 2
    Is your class definitely not in a package? – Jon Skeet May 02 '13 at 09:35
  • Have you tryed to select the project and then go to Run -> Run As menu? there you can chose the project type and the main class – Joan May 02 '13 at 09:35
  • 7+ different answers, all of which the submitters claimed worked suggests that this question is "too broad" – Raedwald Jan 27 '14 at 12:51
  • 50
    7+ different answers, all of which the submitters claimed worked suggests that eclipse is a piece of cr*p that gives the same idiotic error message for 7+ different errors. – J. Ed Apr 27 '14 at 10:24
  • 4
    Raedwald, do you even forums? How could I have made my question any more specific? The fact that it's received more than 25,000 views is an indication that many people suffer from this problem. sJhonny -- totally agree with ya mate. Eclipse turns me off from programming in Java. Oh, and by the way, if anyone's wondering: exactly a year on since I posted this question... I still haven't solved the problem. lel – Fezzo May 03 '14 at 12:18
  • In my case, when the project had build path issue, this happened. – whoami Apr 10 '15 at 01:32
  • http://stackoverflow.com/questions/11235827/eclipse-error-could-not-find-or-load-main-class/31711547#31711547 – Alisa Jul 29 '15 at 21:35
  • 1
    Project -> Clean will remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function – Girdhar Singh Rathore Oct 07 '16 at 15:49
  • 1
    Project Clean was not a solution in my case - same error. – Joe Aug 16 '17 at 04:33
  • right clicked in my project folder ->build path->use as source worked for me – Aayush Neupane May 21 '20 at 02:27
  • In my case, my lib path missing a jar. that is why it did not compile – Feng Zhang Jan 19 '21 at 00:18

8 Answers8

110

This just happened to me today after updating my JRE. I cleaned the project and it started working again.

Project -> Clean will remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function here.

Community
  • 1
  • 1
Nathan
  • 2,093
  • 3
  • 20
  • 33
98

I just ran into that problem. The cause... not sure. It only happened to me after I added a new JVM. My solution:

  • went to run configurations: - run->run configurations

In the Classpath tab:

  • Select Advanced
  • Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.
starball
  • 20,030
  • 7
  • 43
  • 238
myluco
  • 1,097
  • 6
  • 4
  • @tbeernot I think that what you said can also be an issue, I had a similar problem where my maven project wouldn't update properly so I tried to defer from using maven by just removing the maven libraries and the pom.xml all together and just referencing the libraries locally, after doing so the project stopped running the main classes. I recreated the project without the maven configuration and the classes ran fine afterwards. If you have a pom file configure as a maven project, if you have a maven project, ensure that you have the proper maven configuration –  Mar 29 '15 at 15:32
  • 3
    Add what? ('Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.') There are several options.. – Joe Aug 16 '17 at 04:37
44

I deleted a jar file from the bin directory. Right click on your project - Properties then Libraries tab. There was a red flag in there. I removed the jar file from the Libraries and it worked.

lake
  • 459
  • 4
  • 2
22

It seems that the class is not compiled by Eclipse.

Few pointers could be-

  1. Check if the .class file exists in your output folder.To know your output folder Right Click on Project->Properties->Java Build Path(Check at bottom).
  2. Check if Project->build Automatically is checked in the menu.
  3. Check if the HelloWorld class is in src folder or not.Right Click on Project->Properties->Java Build Path(Check source tab).
Jaydeep Rajput
  • 3,605
  • 17
  • 35
  • Regarding your pointers - 1. The .class file does not exist directly in my output folder - it's located in a subfolder called bin. 2. Yes, it is checked. 3. In my src folder, I can only see HelloWorld.java and no .class file. My class is in the (default package), if that helps at all. – Fezzo May 02 '13 at 11:28
  • Seems this helped: `Project->build Automatically` – mrgloom Jul 03 '17 at 09:10
14

Same Problem occur with me.I went to Project > Properties > Java BuildPath.

There In order of export , I moved up my java/main to the top priority.

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29
Shashank Awasthi
  • 347
  • 1
  • 4
  • 10
4

that's because you guys created the class one time with the main method & after that may be you have been deleted that form or workplace & still some of the files exist ,i will suggest you to create that form or workspace again & then delete it by clicking on it completely,then after that if you created the some class like Runner class try to run it again.

Ahuramazda
  • 427
  • 9
  • 14
2

This happened to me. I noticed that someone said I have to create an entire new WORKSHOP! Why? Because I installed a newer JRE version and that won't allow other previous versions to run on it. So all those old files I have become useless in a way. Not really, Just copy and paste it to new class and change it to an unused class name.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Akeme Ubom
  • 31
  • 1
-8

if your package name is same with your class name this problem will occur.