Spring Boot Program cannot find main class
Asked Answered
S

33

105

I have a program which runs as a Spring boot App in eclipse. The program was running fine. Then i did the following:

Right click on project -> Run As -> Maven Test .

This was accidental. When i then tried to run the program as a spring boot app again, it threw the following error below.

Error: Could not find or load main class com.bt.collab.alu.api.webapp.Application

How do i point the application back to my main class? Thanks

Stoddard answered 11/2, 2015 at 9:47 Comment(5)
com.bt.collab.alu.api.webapp.Application is proper class?Alpenhorn
This is the class where my main is yesStoddard
what spring boot version?Alpenhorn
@chris, Can you share your pom.xml?Adverse
What Maven version do you use?Simmon
A
62

Main class is configurable in pom.xml

<properties>
    <start-class>com.bt.collab.alu.api.webapp.Application</start-class>
</properties>
Alpenhorn answered 11/2, 2015 at 9:50 Comment(3)
The main class can be defined as a start-class element in the pom.xml‘s properties section: <properties> <!-- The main class to start by executing "java -jar" --> <start-class>org.baeldung.DemoApplication</start-class> </properties> IMPORTANT -- >>> Note that this property will only be evaluated if we also add the spring-boot-starter-parent as <parent> in our pom.xml. baeldung.com/spring-boot-main-classSoonsooner
one more check need to be done is make sure don't have any errors in project especially in POM.xml even though main class is declared in pom.xmlSartor
@Soonsooner thanks you saved me a lot of time!Leonialeonid
P
159

I had the same problem. Try this :

Right Click the project -> Maven -> Update Project

Then Re-run the project. Hope it work for you too.

Pash answered 9/9, 2016 at 7:8 Comment(5)
This worked for me as well. The initial (maven) project configuration when using the "New Project" > "spring-boot wizard" seems to be incomplete. (Windows 10, Eclipse Photon Release (4.8.0), Spring 2.1.1)Haynes
This worked for me ,but what is the main cause here ?Sagacious
Potentially it can be caused by using mvn install and afterwards mvn clean to remove the target folder.Academy
This worked for me. This error happened to me after I connect the project with gitHubWherefrom
Initially, this didn't work for me. But after checking the checkbox Force Update ... it did.Franko
A
62

Main class is configurable in pom.xml

<properties>
    <start-class>com.bt.collab.alu.api.webapp.Application</start-class>
</properties>
Alpenhorn answered 11/2, 2015 at 9:50 Comment(3)
The main class can be defined as a start-class element in the pom.xml‘s properties section: <properties> <!-- The main class to start by executing "java -jar" --> <start-class>org.baeldung.DemoApplication</start-class> </properties> IMPORTANT -- >>> Note that this property will only be evaluated if we also add the spring-boot-starter-parent as <parent> in our pom.xml. baeldung.com/spring-boot-main-classSoonsooner
one more check need to be done is make sure don't have any errors in project especially in POM.xml even though main class is declared in pom.xmlSartor
@Soonsooner thanks you saved me a lot of time!Leonialeonid
M
28

Have a look under "Run -> Run Configurations..." in Eclipse. You should delete the new one which you created by mistake, you should still have the existing one.

I suspect it has created a new run configuration for the "Run as Maven Test" and you are now always starting this one.

Morbid answered 11/2, 2015 at 10:22 Comment(3)
Great!. This is persistent solution if you delete other running configurations.Barde
Thank you! In my case, I was converting my project from Maven to Gradle. Everything SEEMED to work until "Run as > Spring Boot app": I got Error: could not find or load main class. I deleted the Run as configuration (which happened to have Maven dependencies), re-created it (3 button clicks) ... and the problem disappeared.Knout
I make maven update and after my application startedCopestone
F
19

If you're using Spring Boot and the above suggestions don't work, you might want to look at the Eclipse Problems view (available at Window -> Show View -> Problems).

For example, you can get the same error (Error: Could not find or load main class groupId.Application) if one of your jar files is corrupted. Eclipse will complain that it can't find the Applications class, even though the bad jar is the root cause.

The Problems view, however, will identify the bad jar for you.

At any rate, I had to manually go to my local mvn repo (in .m2) and manually delete the corrupted jar, and update it (right click on the project in the Package Explorer), Maven --> Update Project... -> OK (assuming that the correct project is check-marked).

Formal answered 18/8, 2016 at 19:28 Comment(2)
I had serious issue with Hibernate, which was throwing various errors. This method helped me a lot! When I turned on Problems window, it clearly stated corrupted .jar file, which I simply deleted within .m2 repository. After this, I rebuilt my project with Maven, and now, everything is working fine. Thanks!Croatia
Your suggestion solved my issue. We can also hit "mvn install" on pom file location which automatically handles dependencies issue. Thanx.Cosine
M
15

I tried all the above solution, but didn't worked for me. Finally was able to resolve it with a simple fix.

on STS, Run Configuration > open your Spring Boot App > Open your configuration, Follow the steps,

  1. In Spring boot Tab, check your Main class and profile.
  2. Then go to classpath tab, In the bottom you will see two checkboxes,one is "Exclude Test Code"(Check this if you do not want to run test classes) and other, "Use Temporary Jar file to specify classpath" (this is necessary).

Save your configuration and run. enter image description here

Masked answered 3/2, 2020 at 8:36 Comment(0)
C
12

This happened to me after i updated the pom (Added some dependencies).

The following step helped me to avoid this error

right click on the project > maven > update project
Cann answered 28/12, 2017 at 4:50 Comment(2)
This helpmed me. Thanks.Witted
I run into the same issue again and this time I did not updated the pom.I just picked an old project and tried to run it but got main class could not be found error. Still 'right click on the project > maven > update project' worksCann
T
7

Intellij

  • close Intellij
  • remove all files related to Intellij (.idea folder and *.iml files)
  • open the project in Intellij as if it was the first time (using open recent won't work, id needs to be done via file -> open)
Tainataint answered 9/9, 2019 at 14:3 Comment(0)
T
5

In case if someone is using Gradle for the build then fix will be by adding the following lines in build.gradle file

apply plugin: 'application'

mainClassName = "com.example.demo.DemoApplication"

Torray answered 24/8, 2018 at 13:7 Comment(0)
B
4

Use spring-boot:run command to start spring boot application:

Precondition: 1. Add following property to pom.xml

<property>    
<start-class>com.package.name.YourApplicationMainClass</start-class>
</property>

2. Build your project

Then configure maven command with spring-boot:run.

Navigation:

Right Click Project | Run As | Run Configuration... | Add new Maven Configuration with command spring-boot:run

Bosomed answered 3/8, 2017 at 12:54 Comment(1)
Thank you! that maven configuration help me to test the final spring boot build applicationOdontoid
A
4

The solution for me was:

  1. Boot Dashboard, right click on your Instance.
  2. Open Config
  3. Tab Spring Boot
  4. Main Type, there was the path to the application file missing...
Anamorphoscope answered 2/12, 2021 at 14:38 Comment(0)
R
3

I know this is pretty late answer. But it might still help some new learners. The Following example is only for springboot with NETBEANS. I had to do the following steps:

Step 1. Follow @Mariuszs answer .

Step 2. Right click on project -> Properties -> RUN. Make sure the Main Class field is has the correct starter class else Click browse and select from the available classes .

Step 3. Click OK-> OK. Thant is all. Thank you.

Rotenone answered 9/11, 2018 at 18:22 Comment(0)
A
2

I have used spring 1.5.3.RELEASE version and given pom dependencies like

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent> 

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
   </dependencies>

And my main class was

@SpringBootApplication
public class SpringAppStarter {


    public static void main(String[] args) {

        SpringApplication.run(SpringAppStarter.class, args);
    }

}

But still while execution of main class I was getting:

Could not find or load main class ... with my class name.

I could see that class was available in the class path and I was using STS IDE.

I was not getting reason for the issue then I tried manual build through maven with

mvn clean install

Still problem was there then i realize that my project has two libraries

  1. Reference Library
  2. Maven Library

What I did:
I have created another mavenRepo folder and build it again and refresh my project. After that it worked and I saw that Maven Library was deleted from project class path.

Issues was while execution it was getting multiple version so it was not able to find proper class to execute my application. Other Solution listed in this page also resolves the issue but if you are doing mistake in

  1. Annotating class
  2. pom entry
  3. jar version etc.

Hope this may resolve problem if somebody get this issue

Adversaria answered 13/9, 2017 at 18:28 Comment(0)
Y
2

deleting old jars from .m2 and updating maven project should be primary step. In most of the cases it resolves the issue.The reason for this issue is mostly corrupted old jars

Yoghurt answered 14/11, 2017 at 19:18 Comment(0)
F
2

In my case the following helped:

Added the following section in pom

  <plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
    </plugin>
  </plugins>

Ran it using maven3 and it worked

Finzer answered 7/2, 2018 at 6:57 Comment(0)
A
2

Encountered the same issue and was able to fix it by the following the steps listed below:

  1. File -> Invalidate Cache and Restart
  2. File -> New -> Project from existing source
  3. Select the pom.xml file just (not the whole project directory) to load the project. Project will be setup automatically.
Ajmer answered 31/7, 2018 at 6:9 Comment(0)
L
2

Error:Could not load or find main class

Project:Spring Boot Tool:STS


Resolve Steps:


  1. Run the project

  1. See in problems tab, it will show the corrupted error

  1. Delete that particular jar(corrupted jar) in .m2 folder

  1. Update project

  1. Run successfully

Logrolling answered 8/10, 2018 at 16:17 Comment(0)
P
2

I was having the same problem just delete .m2 folder folder from your local repositry Hope it will work.

Pindaric answered 17/12, 2018 at 10:42 Comment(0)
C
2

start-class doesn't work for me, I fixed it by adding build plugins to pom.xml, and executions is necessary.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Cloudcapped answered 25/9, 2020 at 3:55 Comment(0)
H
2

There are good answers here, but maybe this one will also help somebody.

For me it happened just after i deleted .idea (with IntelliJ), and reimported the project.

then this issue started, i tried to run mvn compile (just via IDE Maven toolbar), once i fixed few compilation errors, issue disappeared

Homogeneity answered 19/11, 2020 at 9:5 Comment(0)
I
1

If you came across the

error:could not find or load main class in maven project

do this it worked for me:
Reason for the error: The reason for this error is the jar file or dependency to run a maven project is not installed correctly.

Solution for the error: Go to the project folder (eg: D:\office\OIA-service-app)
in the address bar of computer type command cmd and press enter.
In the command prompt type command mvn clean install make sure you have the internet connection.

Ipomoea answered 20/9, 2016 at 12:24 Comment(0)
K
1

If your project packaging type war you could not start. I was using maven assembly plugin with this configuration;

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version><!--$NO-MVN-MAN-VER$ -->
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.ttech.VideoUploaderApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

and my packaging tpe was war i got cannot find main class exception bu i changed packaging to jar it worked;

<groupId>com.ttect</groupId>
<artifactId>VideoUploadListener</artifactId>
<version>1.0.0</version>
<packaging>**jar**</packaging>
Kitti answered 21/8, 2017 at 6:22 Comment(0)
D
1

I also got this error, was not having any clue. I could see the class and jars in Target folder. I later installed Maven 3.5, switched my local repo from C drive to other drive through conf/settings.xml of Maven. It worked perfectly fine after that. I think having local repo in C drive was main issue. Even though repo was having full access.

Doublereed answered 13/2, 2018 at 19:19 Comment(0)
I
1

I was facing the same problem. I have deleted the target folder and run maven install, it worked.

Interlunation answered 6/1, 2019 at 6:27 Comment(0)
P
1

I had similar kind of problem while I was adding spring security dependency to my project , then I deleted my target folder and used maven update again . It's worked for me.

Plater answered 12/5, 2019 at 16:12 Comment(0)
F
1

Even I faced the same issue, later I found that it happened because the maven build operation was not happening properly in my environment. Please check it in your case also.

Filipe answered 5/8, 2019 at 5:38 Comment(0)
R
1

I ran into same error, although i was using gradle build. Delegating IDE build/run actions to Gradle is solved my problem.

Intellij: Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner

Screenshot

Radiotelephone answered 29/9, 2019 at 18:19 Comment(0)
C
1

I got the same error in the "Spring Tool Suite" software.

In my case, I have Java 8 installed and I created a project of Java 11. And when I run the project I got the same error, like " can not find main class ".

Then I created a project in Java 8 and the problem resolved.

Canorous answered 16/11, 2020 at 11:9 Comment(0)
A
1

(Eclipse) Updating maven project was not working for me. What I had to do was to go in Run configurations, and in "Main" tab, in "Project" field, write the whole package name com.bla.blabla.MyMainClass (instead of just the Main class name). The error happened because I have several projects with the same name, but in different packages.

Animalist answered 3/11, 2021 at 10:53 Comment(0)
C
1

if none of above solutions work, try to replace build tag with this inside pom file :

<build>
<pluginManagement>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins></pluginManagement>
</build>
Cavatina answered 18/3, 2022 at 20:24 Comment(0)
O
1

I had this same issue and tried a couple of the solutions and they didn't work. The way I solved it was easy and straight forward.

Right click on the spring boot app, select "Maven" > Update Project, enter image description here check the spring boot app you want to fix and press the OK button. enter image description here After it finishes running I was able to start the app successfully.

Oeildeboeuf answered 7/7, 2022 at 15:33 Comment(0)
G
1

If none above are not working, this could be because you didn't put the folder in trusted location. ( For Intellij IDEA)

To put your project in trusted location simply follow this steps:

  1. Open Settings in Intellij IDEA
  2. Search for "Trusted Location"
  3. Put your Current Project in the Trusted location.
  4. Run the Project
Generalissimo answered 5/6, 2023 at 11:55 Comment(0)
L
0

I am new in spring boot and using STS with maven build. I did all the things mentioned above but won't work for me. I noticed two problems in problem window of STS. There was a missing jar file on the path (C:\Users\UserName.m2\repository\com\jayway\jsonpath\json-path\2.2.0). I did following, the way is not strongly suggested but worked for me.

1) I deleted .m2 folder from the path location

2) Restarted STS

3) Right Click on project folder -> maven -> update project -> checked "Force update of snapshots/releases" -> Click Ok.

Maven updated its dependencies and the problem is solved.

Lamere answered 5/12, 2017 at 12:2 Comment(0)
P
0

I got many sleepless nights with this problem. nothing helped. finally I got "my solution" and want to share here.

In my case (I use IntelliJ IDEA) I got a problem with the file (it was not existing)

{$Project_Dir$}\.idea\encodings.xml

My "application.properties" was encoded in UTF-8 charset. I needed to tell IntelliJ IDEA to encode the files within this charset.

encodings.xml content:

<?xml version="1.0" encoding="UTF-8"?>
  <project version="4">
    <component name="Encoding">
      <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
      <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
      <file url="file://$PROJECT_DIR$/src/main/resources/application.properties" charset="UTF-8" />
   </component>
</project>

I hope this helps someone.

Panjabi answered 20/3, 2023 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.