Configuring a Main Class in Spring Boot

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:

  1. If we define no main class, Spring Boot will search for the main class in the class path.
  2. It will fail, we there is no main class or we have multiple classes with the main method.

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]

[pullquote align=”normal”] 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. [/pullquote]

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.


1. Maven

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:

      <!-- The main class to start by executing java -jar -->

[pullquote align=”normal”]This will only work if we add the spring-boot-starter-parent as the parent in our pom.xml. [/pullquote]


2. Spring Boot Maven Plugin

Another option is to pass the main class as a configuration parameter to the spring-boot-maven-plugin in the plugin:

[pullquote align=”normal”]This is useful if you do NOT use the spring-boot-starter-parent pom. [/pullquote]




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.