aCarella
aCarella

Reputation: 2578

Could not find or load main class in STS/Eclipse

I have a main method in a package in one of my projects. Say, the package is com.ant.car. I am trying to run and/or debug this main method, and I keep getting the error Could not find or load main class com.ant.car.

I've searched this problem, and it seems like I can't figure out what is wrong.

1) I've checked run configurations. In Run->Run Configurations, I've checked that the Main class is com.ant.car.

2) I've checked build path. If I right click on the project, I select Build->Build Path, and under the Libraries tab I make sure there are no missing folders with red Xs next to them.

Not really sure what else to do. Any suggestions?

Upvotes: 8

Views: 60318

Answers (23)

Teena Thomas
Teena Thomas

Reputation: 1

You just have to follow the below steps;

  1. Project -> Clean
  2. Maven -> Update Project -> Force update of Snapshots/Release

This will help in the resolving the error.

Upvotes: 0

Gnk
Gnk

Reputation: 720

I had this problem on eclipse sts. For me the following steps solved the problem.

In Boot Dashboard > left click in your project > Open Config > fill the "Main Type" with your main application.

Upvotes: 1

Twinkle Jaswani
Twinkle Jaswani

Reputation: 11

I tried all the answers but finally what worked for me was deleting the project from eclipse workspace and importing it again.

Upvotes: 1

ntepp jean marc
ntepp jean marc

Reputation: 108

You can try one of the two solutions below: Solution 1: Right click the project -> Gradle -> Refresh Gradle Project.

Solution 2: Add gradle or maven in your environment variable. My problem was that I had not added gradle in my environment variable. Before, make sure Gradle or maven is installed in your OS.

Upvotes: 0

bimlesh prasad singh
bimlesh prasad singh

Reputation: 65

You just need to delete Run Configuration file and restart STS and run command -mvn clean install and try to restart the service

Upvotes: 1

Santhosh Emmadi
Santhosh Emmadi

Reputation: 1

  1. Remove project from STS/Eclipse
  2. Close or Refresh the Eclipse/ STS Eclipse.
  3. Run maven install on pom.
  4. Run the project with your Run configuration. Tried above mentioned steps to resolved issue.

Upvotes: 0

Lenzman
Lenzman

Reputation: 1400

This worked for me to solve the error. (I got this error after removing AWS ToolKit)

  1. Close the Eclipse/ STS Eclipse.
  2. Go to the WorkSpace folder.
  3. Delete the .metadata folder.
  4. Open the eclipse.
  5. Run maven install on pom.
  6. Run the project with your Run configuration.

Upvotes: 0

Wenbo Wang
Wenbo Wang

Reputation: 1

I spent several hours on this issue, finally it is fixed by doing this: Properties -> Java Compiler: uncheck the checkbox "use '--release' option"

Upvotes: 0

Adam Law
Adam Law

Reputation: 542

A quick and easy fix is to directly run your SpringBootApplication class (i.e. Right click, Run As -> Spring Boot App). This runs the app and creates a run configuration automatically.

Upvotes: 0

Ido Bar Lev
Ido Bar Lev

Reputation: 506

It worked for me:
1. Delete metadata from work-space directory.
2. Import the project again, but selected copy to work-space option.

I think the cause for the error was Non-English characters in the original saved directory.

Upvotes: 1

Ratnesh Chauhan
Ratnesh Chauhan

Reputation: 21

Deleting target folder manually and mvn clean install worked for me

Upvotes: 0

SelvaKumar T
SelvaKumar T

Reputation: 81

Try updating project by right clicking on it. Maven -> Update Project

Upvotes: 8

Pawan Gunjkar
Pawan Gunjkar

Reputation: 31

This issue occurs when the main .class file moved or not found because you changed the directory for committed/shared the project into the git or another repository.

To Resolve this issue --> Remove existing run configuration and new one. Find the parent pom.xml or project pom.xml and open cmd/command prompt and run the below commands,

  1. mvn clean install package
  2. mvn eclipse eclipse

Upvotes: 1

Lova Chittumuri
Lova Chittumuri

Reputation: 3323

1 ) Clean the Project
2 ) Enable build automatically Option
3 ) Update the maven project by use the short cut Alt + F5

Upvotes: 0

user2241925
user2241925

Reputation: 351

Project -> Clean

this is working

Upvotes: 19

user2615206
user2615206

Reputation: 71

What worked for me: Menu Project -> Properties In Java Build Path, tab Libraries Delete all libraries with a red [x] next to them.

In my case, problem happened when I switched from Kepler to STS IDE.

Upvotes: 5

Mariusz
Mariusz

Reputation: 76

Today I ran into the same problem and I tried a lot of answers. Nothing helped. Cleaning the project, build automatically is already checked, deleting .metadata, etc.

Eventually I tried this and it worked perfectly:

menu Project -> Properties
Java Build Path, tab Libraries

Remove the JRE System Library from there, press Add Library, take the JRE System Library and press Next. Workspace default JRE and press Finish.

Upvotes: 6

Matt Buckborough
Matt Buckborough

Reputation: 44

Project -> Clean... -> check project not working -> Clean

I already had build automatically set, but forcing STS to rebuild it fixed it.

Upvotes: 1

Kiran Kumar D M
Kiran Kumar D M

Reputation: 1

I think .class files are deleted/missing from JavaProject/bin folder.

To resolve this issue ->

1) Just cut paste and save the code contents of all the files that you are using then .class files will be regenerated.

2) Then run the code and you can see it works fine if there is no syntactical errors.

Upvotes: 0

Dhanu N Joy
Dhanu N Joy

Reputation: 1

I faced the same issue..

just do follow these steps:

STS/Eclipse --> Project --> Enable "Build Automatically"

then refresh your project, it will resolve your issue.

Still not refreshed your projects automatically, just restart your STS and check.

Hope it will help you.

Upvotes: 0

Arul Rozario
Arul Rozario

Reputation: 83

Sometimes the problem can be caused by some newly added dependencies. Try removing the recently added jars. That's how I fixed my problem.

Upvotes: 1

aCarella
aCarella

Reputation: 2578

The solution to this was the following:

  • Close Eclipse/STS
  • Use a file explorer on your operating system to navigate to your workspace (In my case, I'm on Windows so I used Windows Explorer)
  • Delete the .metadata directory (or to be safe, copy the directory somewhere else to be safe, then delete it)
  • Restart Eclipse/STS

Is there a more improved answer than this? I don't want to look like I'm trying to boost my own reputation points, so if someone can provide a better answer then please do so.

Upvotes: 1

user3624540
user3624540

Reputation: 56

The Error "Could not find or load main class com.ant.car" occur when your class is not in build path. please make sure that .class file is generated and it's in build path ..

Upvotes: 0

Related Questions