Spring Boot – Which main class to start

If Spring Boot project contains multiple main classes, Spring Boot will fail to start or packag for deployment.

Terminal

$ mvn package 
#or
$ mvn spring-boot:run

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) 
Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run failed: 
Unable to find a single main class from the following candidates 
 [com.mkyong.Test, com.mkyong.SpringBootWebApplication] -> [Help 1]

Maven example

1.1 Define single main class via start-class properties

pom.xml

  <properties>
      <!-- The main class to start by executing java -jar -->
      <start-class>com.mkyong.SpringBootWebApplication</start-class>
  </properties>

1.2 Alternatively, define the main class in the spring-boot-maven-plugin

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.mkyong.SpringBootWebApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

References

  1. Spring Boot – The executable jar format
  2. Spring Boot Maven Plugin – Usage

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
Rachaita ChakravartyShrikant Mudholkarrajsekhar mahapatroStephane Segning Lambouscott Recent comment authors
newest oldest most voted
Rachaita Chakravarty
Guest
Rachaita Chakravarty

Which is the main class for a spring application.

Shrikant Mudholkar
Guest
Shrikant Mudholkar

Should I use a common spring boot main class in a library that my other spring microservices pull?

rajsekhar mahapatro
Guest
rajsekhar mahapatro

How does it pick if i just have one main class but i am not using the @SpringBootApplication annotation?

QiYifei
Guest
QiYifei

cool

Alan Barboza
Guest
Alan Barboza

I’m getting “java.lang.ClassNotFoundException” when try to run application with “mvn spring-boot:run” command

scott
Guest
scott

you should cut what class not found, that we can help to find the problem

Stephane Segning Lambou
Guest
Stephane Segning Lambou

The main class isn’t found for my case : java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

Neha
Guest
Neha

Thank you. Great