Eclipse "Error: Could not find or load main class"

690,171

Solution 1

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"/>

Solution 2

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:
For me, the fix was to go to the run configurations, (Green Play Button -> Run Configurations) and remove all references to the class. The next time you run

"Run As" -> "Java Application"

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

Solution 3

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

Solution 4

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 )

Solution 5

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.

Share:
690,171
Chris
Author by

Chris

Updated on December 18, 2021

Comments

  • Chris
    Chris over 2 years

    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.

  • Yeung
    Yeung over 10 years
    yep. after I move the project to git. The problem occur. I try this and I also need to "close project" and "open project" to do the trick.
  • NixRam
    NixRam over 9 years
    Thanks..!! This one worked for me. The problem started when I changed the name of the project.
  • nsof
    nsof about 9 years
    Same thing for me - also don't know why this happened in the first place.
  • Nicholas Miller
    Nicholas Miller about 9 years
    +1 public static void main(String[] args) is what got me. I accidentally had the wrong signature (Main instead of main). So, it is important to make sure the signature is correct. :)
  • Luís de Sousa
    Luís de Sousa about 9 years
    This fixed it for me. You do not need to shut down Eclipse to change the .project file, you can simply close the project.
  • CodeMed
    CodeMed about 9 years
    I had to experiment with a few ways of removing Run Settings from within the Properties dialog box. But this is the answer that set me on the right path. +1
  • CodeMed
    CodeMed about 9 years
    In eclipse, right clicking on Run As..Maven clean, then Run As..Maven install did it for me. The problem started when I changed an eclipse java project to become an eclipse maven project. +1 for pointing in the right drection.
  • Greg
    Greg about 9 years
    I had copied a run configuration because I wanted to duplicated the JVM arguments for a different class.
  • Amos M. Carpenter
    Amos M. Carpenter about 9 years
    Welcome to SO, DeeCod. Deleting the bin folder and doing a "project > clean" and build has already been suggested in other answers. Rather than posting a "me, too" as an answer, I'd recommend getting enough reputation (you only need 50) to post comments and confirming that a particular approach worked for you in a comment.
  • DeeCode
    DeeCode about 9 years
    Thanks Amos! Anyway Im sorry if im wrong, as far as i see -> Deleting the bin before Clean/ rebuild is not suggested in other answers in this thread... Just Clean > build did not work in my scenario... as long as this helps someone :)
  • Amos M. Carpenter
    Amos M. Carpenter about 9 years
    No worries - I've come across plenty of these scenarios where trying combinations of strange things suddenly works for no apparent reason. ;-)
  • Muhammad Gelbana
    Muhammad Gelbana almost 9 years
    This will unbind my project from the workspace. You'll have to import all your projects again !
  • Aequitas
    Aequitas over 8 years
    How would one change this?
  • aCarella
    aCarella almost 8 years
    How do we "remove all references to the class"?
  • Shahar
    Shahar almost 8 years
    I, on the other hand, just shut down Eclipse (but did not edit .project file. Turned Eclipse back on, and it worked: error was gone.
  • randombee
    randombee over 7 years
    Worked like a charm. My problem was that I created a build.gradle and settings.gradle that were changing the default names that were associated to the project. Removing the references (run as -> run configurations -> remove all configurations associated to the project)
  • benzabill
    benzabill over 7 years
    worked for me! I also had an exclamation mark next to my Project folder name. This fixed that too
  • TYMG
    TYMG about 7 years
    I agree with @MuhammadGelbana, i deleted the .metadata and it blew my workspace. Be very careful when trying this solution.
  • Dhruv Kaushal
    Dhruv Kaushal over 6 years
    Worked for me! Thanks
  • dnuttle
    dnuttle about 6 years
    This worked for me. I encountered the problem after upgrading to a newer version of Eclipse. It's a gradle project, and gradle clean eclipse did not resolve the problem. A simple refresh did.
  • jcool
    jcool about 6 years
    "remove all references to the class" - shall I remove all run configurations ?
  • JayC
    JayC about 6 years
    I do not have external jars but I am facing similar issues. stackoverflow.com/questions/49738153/…
  • Andrew Norman
    Andrew Norman over 5 years
    this worked by simply removing and re-adding the same java JRE library
  • gkubed
    gkubed over 5 years
    @Aequitas do "Run" -> "Run As" -> "Java Application." once, and it will set the class path automatically. See Blamkin86's answer. Classpath settings may be found under "Run" -> "Run Configurations...".
  • Michał Leon
    Michał Leon over 5 years
    That worked for me even though there was no warning nor exclamation mark
  • Chris Halcrow
    Chris Halcrow over 5 years
    To answer the question "How do we remove all references to the class" - all I had to do is go the the run configurations, and delete the package-qualified class name from under the 'Main' tab (in the section 'Main class')
  • Amarth Gûl
    Amarth Gûl over 5 years
    I overwrite the .classpath and the .project file with files from another project, and it worked... is that a coincident or the classpath is really what matters?
  • Brent Sandstrom
    Brent Sandstrom over 5 years
    Probably a coincidence since those files are specific to your project. You may have unknowingly made eclipse regenerate those files by introducing errors. Open the file and read it and you'll get an idea of what they do. Classpath contains libs and src folder directories and .project stores plugins and project facet stuff
  • vikifor
    vikifor about 5 years
    Removing run configuration and main class setting helped to me:)
  • Rohan Singh
    Rohan Singh over 4 years
    Bro you literally just saved my AP Java Project you have no idea thank you so much
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 4 years
    -U,--update-snapshots Forces a check for missing releases and updated snapshots on remote repositories
  • Jonathan Benn
    Jonathan Benn over 4 years
    @aCarella I deleted the run configuration that referred to the problem class, and this worked for me!
  • mrswadge
    mrswadge over 4 years
    I found a simple "Hello World" application wasn't working. Eventually I realised that it was a specific library on the build path that was preventing the simplest of tests. I found the reason was it was generating a very long classpath. Inside the Classpath tab in Run Configurations, there is now a box Use temporary JAR to specify classpath. Enabling this setting got it working.
  • jumps4fun
    jumps4fun about 4 years
    My java installations were unbound, due to central company updates. This wasn't exactly the fix i needed, but good enough to make me find the problem and solve it. Thanks.
  • Rahul
    Rahul almost 4 years
    Worked for me. Thanks!
  • JohnP2
    JohnP2 over 3 years
    loved this: Delete the metadata folder inside your workspace I was able to fix it for myself by making a new workspace
  • user3217883
    user3217883 over 3 years
    I used mvn compile and that fixed eclipse
  • Smart Coder
    Smart Coder over 3 years
    project properties -> removing previously missing jars helped fix it.
  • Anurag
    Anurag over 3 years
    "red exclamation mark" appears in one of my others projects, closing that project and running desigered project didn't help me.
  • Ashutosh Tiwari
    Ashutosh Tiwari about 3 years
    I just clicked on add. And finish, after which it worked, don't know what exactly happened but it did worked :)
  • pankaj
    pankaj almost 3 years
    This worked for me as well but what is meant by classpath getting over the limit? Never faced such type of issue before.
  • xbranko
    xbranko almost 3 years
    On Windows the OS limit for the command line length is 32KB. For small apps with few dependencies this is not a problem, but for enterprise apps that may have hundreds of jars via direct and transitive dependencies, that quickly becomes an issue.
  • dom
    dom over 2 years
    this worked like a charm for me too after struggling for days to get it work
  • jacob12
    jacob12 about 2 years
    Thanks alot... my eyes are blistering with eyesore after trying out all of the above answers through half of the day and nothing worked. However, yours did. Strangely, now every download project runs clean without any problems of 'could not find or load main class' , perhaps the problem in my case was related to the workspace's metadata?