Eclipse "Error: Could not find or load main class"
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:
- Right click on your Project > Properties > JavaBuildPath > Libraries.
- Remove the jar file, having a red flag on it.
- If problem persists try the solution below. This worked for me when I faced this problem second time:
- Right-Click Project > Properties > Java Build Path > Libraries
- Remove Library
- 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.
Chris
Updated on December 18, 2021Comments
-
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 over 10 yearsyep. 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 over 9 yearsThanks..!! This one worked for me. The problem started when I changed the name of the project.
-
nsof about 9 yearsSame thing for me - also don't know why this happened in the first place.
-
Nicholas Miller about 9 years+1
public static void main(String[] args)
is what got me. I accidentally had the wrong signature (Main
instead ofmain
). So, it is important to make sure the signature is correct. :) -
Luís de Sousa about 9 yearsThis fixed it for me. You do not need to shut down Eclipse to change the
.project
file, you can simply close the project. -
CodeMed about 9 yearsI 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 about 9 yearsIn eclipse, right clicking on
Run As..Maven clean
, thenRun 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 about 9 yearsI had copied a run configuration because I wanted to duplicated the JVM arguments for a different class.
-
Amos M. Carpenter about 9 yearsWelcome 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 about 9 yearsThanks 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 about 9 yearsNo worries - I've come across plenty of these scenarios where trying combinations of strange things suddenly works for no apparent reason. ;-)
-
Muhammad Gelbana almost 9 yearsThis will unbind my project from the workspace. You'll have to import all your projects again !
-
Aequitas over 8 yearsHow would one change this?
-
aCarella almost 8 yearsHow do we "remove all references to the class"?
-
Shahar almost 8 yearsI, 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 over 7 yearsWorked 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 over 7 yearsworked for me! I also had an exclamation mark next to my Project folder name. This fixed that too
-
TYMG about 7 yearsI agree with @MuhammadGelbana, i deleted the .metadata and it blew my workspace. Be very careful when trying this solution.
-
Dhruv Kaushal over 6 yearsWorked for me! Thanks
-
dnuttle about 6 yearsThis 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 about 6 years"remove all references to the class" - shall I remove all run configurations ?
-
JayC about 6 yearsI do not have external jars but I am facing similar issues. stackoverflow.com/questions/49738153/…
-
Andrew Norman over 5 yearsthis worked by simply removing and re-adding the same java JRE library
-
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 over 5 yearsThat worked for me even though there was no warning nor exclamation mark
-
Chris Halcrow over 5 yearsTo 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 over 5 yearsI 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 over 5 yearsProbably 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 about 5 yearsRemoving run configuration and main class setting helped to me:)
-
Rohan Singh over 4 yearsBro you literally just saved my AP Java Project you have no idea thank you so much
-
Kanagavelu Sugumar over 4 years-U,--update-snapshots Forces a check for missing releases and updated snapshots on remote repositories
-
Jonathan Benn over 4 years@aCarella I deleted the run configuration that referred to the problem class, and this worked for me!
-
mrswadge over 4 yearsI 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 about 4 yearsMy 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 almost 4 yearsWorked for me. Thanks!
-
JohnP2 over 3 yearsloved this: Delete the metadata folder inside your workspace I was able to fix it for myself by making a new workspace
-
user3217883 over 3 yearsI used mvn compile and that fixed eclipse
-
Smart Coder over 3 yearsproject properties -> removing previously missing jars helped fix it.
-
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 about 3 yearsI just clicked on add. And finish, after which it worked, don't know what exactly happened but it did worked :)
-
pankaj almost 3 yearsThis worked for me as well but what is meant by classpath getting over the limit? Never faced such type of issue before.
-
xbranko almost 3 yearsOn 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 over 2 yearsthis worked like a charm for me too after struggling for days to get it work
-
jacob12 about 2 yearsThanks 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?