Spring Boot Main Class Configuration


A spring boot application starts from the main class (class with main() method) that contains main method with below signature. On Startup of spring boot application it create Spring ApplicationContext and beans also for application..

@SpringBootApplication
public class SpringBootMainClassApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMainClassApplication.class, args);
    }
}

By default spring boot search the main class in the class path at compile time. if it’s find multiple main class or none then it will throw exception as below.

Spring Boot Application without Main class

While running the application through STS/Eclipse it will show popup as “Selection does not contain a main type”.

Spring Boot Application without Main Class

Spring Boot Application with multiple main class

If you run the application manually through STS/Eclipse then you have to select one of the Main class below before execution. If you are trying to maven package of install from CMD it will throw exception as below.

mvn package -Dmaven.test.skip=true

it will through exception as below in CMD.

Spring Boot Application with Multiple Main Class

Solutions

To start a Spring Boot application, it’s mandatory to specify one Main class in your class path. If there is one Main class as above then will not be any issue to start a Spring boot application.

Now if there are multiple main class in your Spring Boot application then below solutions will help to start your application.

Solution 1: Specify the Main class in pom.xml properties

You can specify your main class in property start-class as below.

<properties>
      <!-- The spring boot main class to start by executing "java -jar" -->
      <start-class>com.FacingIssuesOnIT.SpringBootMainClassApplication</start-class>
</properties>

Note : This property configuration will help only when you added the spring-boot-starter-parent as in your pom.xml otherwise follow the solutions 2 or 3 to configure the main class.

Solution 2 : Specify the Main class in spring-boot-maven-plugin of pom.xml

You can also specify the main class of Spring Boot in spring-boot-maven-plugin of pom.xml as below:

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

Solution 3: Specify the Main Class on run time through CLI

You can also specify the Spring Boot Main class on runtime through CLI as below:

java -cp spring-boot-app.jar -Dloader.main=com.FacingIssuesOnIT.SpringBootMainClassApplication org.springframework.boot.loader.PropertiesLauncher

By using any of these configuration, You can decide you Spring Boot application main class for start the application.

Conclusion

In this post you learn about the different way to configure Spring Boot Application main class to start the application if your application having multiple Main classes.

Let me know your thoughts on this post.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s