80

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!

11
  • 2
    Is your class definitely not in a package? Commented May 2, 2013 at 9:35
  • 51
    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. Commented Apr 27, 2014 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 Commented May 3, 2014 at 12:18
  • 1
    Project -> Clean will remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function Commented Oct 7, 2016 at 15:49
  • 1
    Project Clean was not a solution in my case - same error. Commented Aug 16, 2017 at 4:33

8 Answers 8

111

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly for me. I had a problem where Eclipse threw that error because I had manually deleted some *.class files. I like this solution much better than fiddling around with this or that configuration option. Simply use Project -> Clean... and voilà!
I started my very first Java project, and got this error. I guess it allows you to run without ever building. Ah, Eclipse, why you gotta be that way? This worked, thank you.
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.

2 Comments

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

6 Comments

After days or searching, this enlighting answer was found. Thanks a zillion times over :)
@lake it worked for me,can you please explain how a unused jar leads to this kind of problem
Works for me!!!! Awesome!
Wish I could give you more credits. saved me a lot of time
I added proper jar files and this error is gone.
|
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).

2 Comments

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.
Seems this helped: Project->build Automatically
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.

Comments

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.

2 Comments

Deleting all Eclipse created files and the workspace seems to have worked for me.
this was my problem - using the scala ide
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.

1 Comment

This is the only solution that has worked for me. Thank you, @Akeme
-8

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.