117
votes

I have a project in eclipse on my laptop that I pushed to Git https://github.com/chrisbramm/LastFM-History-Graph.git

It works fully on my laptop and runs/builds without a problem but on my desktop it doesn't Eclipse gives the error

Error: Could not find or load the main class lastfmhistoryguis.InputPanel

I've tried building the project from:

Project>Build Project

But nothing happened. I've set the PATH variables on this computer to JRE6, JRE7 and JDK 1.7.0 even though these aren't set on my laptop.

I did have Jar file (last.fm-bindings-0.1.1.jar) that was in my .classpath file that was in C:\Users\Chris\Downloads folder on my laptop hence it wasn't included in the git tree which I recently brought into the project folder and committed ,but I'm not sure whether I have done it right. Would this also be causing a problem but there isn't a main argument in there.

I can't work out now, what I need to check/change.

30
Did you try Project>Clean and Project>Build Project?javaCity
Multiple times and it doesn't seem to have workedChris
system environmental classpath includes . Where else do I need to change and what specifically would I do?Chris
In my case I was running java .\bin\selenium-server-standalone-3.9.1.jar when it should of been java -jar .\bin\selenium-server-standalone-3.9.1.jarCoty Embry

30 Answers

44
votes

In your classpath you're using an absolute path but you've moved the project onto a new machine with quite possibly a different file structure.

In your classpath you should therefore (and probably in general if you're gonna bundle JARS with your project), use relative pathing:

In your .classpath change

<classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/><classpathentry kind="lib" path="C:/Users/Chris/Downloads/last.fm-bindings-0.1.1.jar" sourcepath=""/>

to

<classpathentry kind="lib" path="last.fm-bindings-0.1.1.jar"/>
77
votes

If you create a java class with public static void main(String[] args), Eclipse will run that main method for you by right clicking on the file itself, or on the file in the project explorer, then choosing:

"Run As" -> "Java Application."

Once you do this, Eclipse stores information about your class, so you can easily run the class again from the Run As menu (Green Play Button on the toolbar) or from the Run Configurations dialog.

If you subsequently MOVE the java class (manually, or however), then again choose

"Run As" -> "Java Application,"

from the new location, Eclipse will run the original stored configuration, attempt to invoke this class from its original location, which causes this error.


SOLUTION:

"Run As" -> "Java Application"

Eclipse will write a new configuration for the moved class, and the error will go away.

61
votes

tl;dr: Clean your entire Build Path and everything you ever added to it manually. This includes additional sources, Projects, Libraries.

  • Project -> Clean
  • Make sure Project -> Build automatically is active
  • Project -> Properties -> Java Build Path -> Libraries: Remove any external libs you have ever added. Don't remove standard libraries like the JRE System Library.
  • Try to run your main class now. The "class could not be found / load" error should be gone. Try adding your external libs/jars one after each other.

Reason behind this: The compiler had issues linking the libraries to the project. It failed and produced a wrong error message.

In my case, it should have been something like "Could not add AutoHotkey.dll to the build path" because that was what made the compiler fail.


If this is still not working, have a look at the built-in ErrorLog of Eclipse:

Window -> Show View -> General -> Error Log

32
votes

I did all the things mentioned above, but none of them worked for me

My problem resolved as follows:

  1. Right click on your Project > Properties > JavaBuildPath > Libraries.
  2. Remove the jar file, having a red flag on it.
  3. If problem persists try the solution below. This worked for me when I faced this problem second time:
    1. Right-Click Project > Properties > Java Build Path > Libraries
    2. Remove Library
    3. Add Library. (Choose the JRE System Library )
19
votes

I faced similar problem in my maven webapp project after spending nearly one hour , I found a solution which worked for me .I typed the following maven command and It worked

mvn clean install -U
I dont know the exact reason behind it.

9
votes

I am assuming that you had imported the project into your desktop eclipse installation? If that is the case, you should just select Project > Clean. Then rebuild your project. Worked like a charm for me.

9
votes

VERY CAREFUL: This will unbind your project from the workspace. You'll have to import all your projects again

I had the same issue and solved it using:

Eclipse Mars
Egit
Github
Maven Project

The Problem was that i made my maven project available to github. It moved my project to my github folder.

Solution:

  • Close Eclipse
  • Delete the metadata folder inside your workspace
  • Restart Eclipse

Start screen will be displayed.

  • Close the start screen
  • Rightclick into package explorer
  • Chose "import maven project",
  • Navigate to your github folder and import the maven project.

After this my project compiled with success.

7
votes

Check that your project has a builder by either:

  • check project properties (in the "package explorer", right click on the project, select "properties"), there the second section is "Builders", and it should hold the default "Java Builder"
  • or look in the ".project" file (in .../workspace/yourProjectName/.project) the section "buildSpec" should not be empty.

There must be other ways, but what I did was:

  • shut down eclipse
  • edit the ."project" file to add the "buildSpec" section
  • restart eclipse

A proper minimal java ".project" file should look like:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
        <name>myProjectName</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
                <buildCommand>
                        <name>org.eclipse.jdt.core.javabuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
        </buildSpec>
        <natures>       
                    <nature>org.eclipse.jdt.core.javanature</nature>
        </natures>      
</projectDescription>
7
votes

Well the following worked for me...

  1. Went into the project folder (inside workspace)
  2. Then, deleted the bin folder
  3. Then, Cleaned project / projects (in Eclipse)
  4. built/run from Eclipse.
5
votes

Similar thing happened to me few times, the only way I knew to fix this was to remove the metadata folder. Fortunately I have found another way.

Try going to project properties > Java Build Path > Order And Export tab > select all (or try to play with check boxes there).

This should cause complete project rebuild and Eclipse to see main class.

Addition: I have noticed that this bug occurs when you have many projects in a work space and some of them is configured wrong(red exclamation mark appears). Fixing project build path and other settings(even if this project is not related to the one you have problems with) should fix an issue.

5
votes

For me, the reason that this error started showing up was due to classpath getting over the limit on windows. Then I discovered the option "Use temporary JAR to specify classpath (to avoid classpath length limitations)". Selecting this option fixed the problem for me. The option resides in Run/Debug Configuration, Classpath tab, see the image below.

enter image description here

4
votes

My Main class could not be found or loaded problem is caused by an interesting reason.

In our project, we are using Maven as build tool and my main class extends a class, which is on the class path but its scope was test, while the main class is not under the test package.

If your main class extends a class, first try to run your main class by removing extends part. If it runs, you will at least understand that the problem is not because of run configuration or eclipse but the class, your main class extends.

4
votes

If your code is good and you know you're having an Eclipse problem, this will solve it.

You could simply delete $yourproject/.classpath , $yourproject/.project , and $yourworkspace/.metadata. Someone else mentioned this option. It will blow up your entire workspace though. Instead:

  1. Delete .classpath and .project from your project
  2. Delete your project in eclipse. DO NOT check delete project contents on disk.
  3. Now, in a file explorer, go into $yourworkspace/.metadata.
  4. Search for $yourprojectname
  5. Delete everything you find. It should be safe-ish to delete anything in the .metadata directory.
  6. In eclipse: File > Import > General > Projects from Folder or Archive > $yourproject > finish
  7. Right click your project > properties > Java Build Path > Source tab
  8. Select all source folders, remove.
  9. Add folder, select src (whatever your src folder is called) and add it
  10. Go to libraries tab
  11. Add any jars to your build path here. There should be no more errors on your project now.
  12. Run your project like you normally would.
4
votes

this could cause of jdk libraries if you had imported into jre

this happen to me , so check installed jre jars

in eclipse click on Windows > Preferences > Java > Installed Jres > click on Jre and edit after that look into jar list make sure none is of jdk or corrupted , enter image description here

3
votes

This problem is also caused when you have special characters in your workspace path. I had my workspace in my personal folder and its name was in Greek, so it didn't work. I changed my workspace, now contains only english characters in its path, and now the project is built without any problems.

3
votes

If You are using eclipse then the following steps will solve your problem:

Go to Run -> Run Configurations -> Main Class Search -> Locate your class manually -> Apply -> Run

3
votes

I had this error. It was because I had static void main(String[] args)
instead of public static void main(String[] args)

I spent nearly an hour trying to figure that out.

Note: The only difference is that I didn't declare main to be public

3
votes

To solve this error do the following steps:

Step 1: Open the .project file.

Step 2: Check two tags...

    a) <buildSpec>

    b) <natures>

Step 3: If the above-mentioned tags do not include any content then surely the above error is going to occur.

Step 4: Add the following content to each tag in order to resolve the above error.

For <buildSpec> :

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

For <natures> :

<natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
</natures>

Step 5: Save your file by hitting ctrl + s.

Step 6: For safe side just close the project once and reopen it.

Step 7: That's it. You are ready to rock!!!

Please mention in comments if the solution was helpful.

Thank you.

3
votes

I had the same problem with correct .classpath file, and soon found actually it's not the .classpath file counted (after I fixed this issue, I replace the workable .classpath file with the original one, the project still worked, which means the .classpath file was not the case)

Since it's a Maven project, all I did is:

  1. mvn eclipse:clean
  2. delete eclipse project
  3. import the project
  4. done
2
votes

I run into the same problem, but in my case it was caused by missing (empty) source folder (it exists in original project, but not in GIT repository because it's empty).

After creating the missing folder everything works.

2
votes

I ran into this error today because I set up a hello world program and then cut and pasted a new program into the same file. To fix the problem of not finding hello world as the last was called I clicked Run-> Run Configurations and then under Main Class I clicked search and it found my new class name and replaced it with the correct new name in the text that I pasted. This is a newbie problem I know but it is also easy to fix. I hope this helps someone! Douglas

2
votes

Mostly this happens, because Eclipse cleans the .class files, but don't build them again. Check the bin folder, it should be empty. Then you should check, is there anything else, which is causing build ti fail. You might have added some jars in classpath, which Eclipse might not be able to find.

2
votes

Just go to your Package Explorer and press F5, or for some laptops fn+F5. The reason is that eclipse thinks that the files are somewhere, but the files are actually somewhere else. By refreshing it, you put them both on the same page. Don't worry, you won't lose anything, but if you want to be extra careful, just back up the files from your java projects folder to somewhere safe.

2
votes

These are the simple steps, which helped me to solve this problem.

  1. Close the eclipse
  2. Delete ".metadata" folder in your work-space. (may be hidden folder)
  3. Open the eclipse (it will automatically create ".metadata" folder in your work- space)
  4. Try to run the program.
2
votes

Check the workspace error log (Windows-> Show View -> Error log). If you see any of the jar's imported is corrupted, remove the corresponding repository folder and re-import again.

2
votes

2 types of solutions exits for the same.

(1) Go 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) If first solution is not working then it means the jar you are pointing out to your project is taking the path of your local Maven repo which is not getting updated to your project so better you check the jar from that local maven repo and copy it paste it into new project simply or just download it from any site and configure it into your build path.

I hope it helps.

2
votes

I received this error as well, just after moving some resources. Checking the error log, I saw that Eclipse couldn't make a build since it couldn't remove a file/folder. Try manually removing the "bin" (or whatever it's called for you) folder.

1
votes

I just had this problem after first having the problem of Windows 8 refusing to update my path no matter what I set JAVA_HOME to - java -version reported the last JDK instead of the one I stored in JAVA_HOME. I finally got that to work by putting '%JAVA_HOME%/bin;' at the front of the path environment variable instead of at the end. Then I launched Eclipse and all the sudden it could not find my main class when it worked fine before this. What I did to fix it was went into the project properties, removed the existing JRE library from the libraries tab, added a new JRE by selecting the "Add Library" button and then followed the prompts to install JRE 7 as my default JRE. Now all is back to working.

1
votes

I found other solution in my case this problem: Eclipse->Preferences->Java->Installed JRE then press button Search. Select folder in Linux /usr then Eclipse found all JVM.

Select another JVM too current. It is solved for my case.

1
votes

I had the same problem after I created new package("tables") in my project.

I went to Window -> Show View -> General -> Error Log and Ive read error:

JavaBuilder handling ImageBuilderInternalException while building: PizzaService

org.eclipse.core.internal.resources.ResourceException: Resource '/PizzaService/bin/tables' already exists.

as it turned out I had a text file in another source folder with the same name as this new package. So I've changed text file name from "Tables" to "Tabless" and I could run my project again.

Hope this helps.