In this post of Spring Boot, we will learn how to configuring a main class in Spring Boot. We will look at the different options to tell Spring Boot which is the main class to use in the executable jar.
Spring Boot bootstrap or start the application using the main class. Spring Boot main class start up the Spring ApplicationContext. When Spring boot application starts:
If we try to run our application without main class, we will get the following exception on the startup:
Error: Main method not found in class com.javadevjournal.SwaggerRestApiApplication, please define the main method as: public static void main(String args) or a JavaFX application class must extend javafx.application.Application
If our application contains over one class with the main method, we will et the following error on startup:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) on project swagger-rest-api: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run failed: Unable to find a single main class from the following candidates [com.javadevjournal.SwaggerRestApiApplication, com.javadevjournal.OneMoreMainClass] -> [Help 1]
You may not face this issue, if you are using IDE to run the application by running specific main class. Here you are executing specific main class.
Spring Boot does not work in traditional ways of supplying the main class name property in
META-INF/MANIFEST.MF.In Spring Boot application, we need to define the start class in the
JarLauncher to start the application. In case me have multiple classes with the main method, let’s see what are the different options to configuring a main class in Spring Boot.
The recommended way it to set the main class in our
pom.xml file.We can define the main class as a start-class element in the
pom.xml‘s properties section:
<properties> <!-- The main class to start by executing java -jar --> <start-class>com.javadevjournal.SwaggerRestApiApplication</start-class> </properties>
This will only work if we add the
spring-boot-starter-parentas the parent in our
Another option is to pass the main class as a configuration parameter to the
spring-boot-maven-plugin in the plugin:
This is useful if you do NOT use the spring-boot-starter-parent pom.
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.javadevjournal.SwaggerRestApiApplication</mainClass> </configuration> </plugin> </plugins> </build>
There are multiple ways to configuring a main class in Spring Boot.In this short post, we saw three different options to tell Spring Boot which is the main class to use in the executable jar.
Manish's primary interests are Java, Spring Boot and Spring. His focus is more toward the automations and testing.Manish love travelling and when not working, he might be exploring some new destination.
Apache Camel with Spring Boot26 May, 2020
Introduction to Spring Cloud Netflix and Eureka12 May, 2020
Microservices with Spring Boot – Building Microservices Application Using Spring Boot14 Apr, 2020
Spring Boot With MongoDB18 Feb, 2020
Spring Boot Configuration Properties04 Feb, 2020
Multi-Module Project With Spring Boot