Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot: Unable to find a single main class from the following candidates

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine.Technologies used: Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8

I have an SpringBoot app. with these 2 classes:

@Profile("!war") @SpringBootApplication @Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class}) public class BookApplication {      public static void main(String[] args) {         SpringApplication.run(BookApplication.class, args);     } }  @Profile("war") @Import({SecurityConfig.class ,PersistenceConfig.class}) @SpringBootApplication public class BookApplicationWar extends SpringBootServletInitializer {      @Override     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {         return application.sources(BookApplicationWar.class);     }      public static void main(String[] args) throws Exception {         SpringApplication.run(BookApplicationWar.class, args);     }  } 

I generate the war with this command

 mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPP-KEY=pebloc 

But I got this error:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project book: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.tdk.BookApplication, com.tdk.BookApplicationWar] -> [Help 1] 
like image 592
Nunyet de Can Calçada Avatar asked Mar 16 '17 17:03

Nunyet de Can Calçada


People also ask

How does Spring Boot find main class?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

How do I run multiple main classes in spring boot?

Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM. xml for Spring Boot to identify the main class of the application. Voila!

Why does a spring boot application need a main () method?

2 Answers. Show activity on this post. Method main is an entry point for standalone applications, so if you want to use spring boot standalone application, usually (if not always) packaged into a JAR - then yes, you should use main method.


1 Answers

If you have more than one main class, you need to explicitly configure the main class in each profile:

<profiles>     <profile>         <id>profile1</id>         <properties>           <spring.boot.mainclass>com.SomeClass</spring.boot.mainclass>         </properties>     </profile>     <profile>         <id>profile2</id>         <properties>           <spring.boot.mainclass>com.SomeOtherClass</spring.boot.mainclass>         </properties>     </profile> </profiles> 

...

<plugin>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-maven-plugin</artifactId>     <version>1.5.2.RELEASE</version>     <executions>       <execution>         <goals>           <goal>repackage</goal>         </goals>         <configuration>           <mainClass>${spring.boot.mainclass}</mainClass>         </configuration>       </execution>     </executions>     ... </plugin> 

See spring-boot:repackage

like image 90
Sean Patrick Floyd Avatar answered Sep 21 '22 15:09

Sean Patrick Floyd