In this post, we will explore different ways of specifying the main class in a Spring Boot application.

Spring Boot creates executable JAR or WAR files that can be run as standalone applications, without requiring any external application server or container. However, to create an executable JAR or WAR file, Spring Boot needs to know which class is the main class of the application. The main class is the class that contains the public static void main(String[] args) method that starts up the Spring ApplicationContext and launches the application.

By default, Spring Boot will scan the classpath and try to find a single main class, but this may not always work, especially if the project has more than one class with a main method. In such cases, we need to specify the main class explicitly, using some configuration options that Spring Boot provides.

1. Using start-class property

One of the simplest and most intuitive ways of specifying the main class in a Spring Boot application is by using the start-class property. If we’re using the spring-boot-starter-parent, we can easily do this by overriding the start-class property in pom.xml. For example, let’s say we have a main class called com.example.HelloWorldApplication, and we want to specify it as the start-class of our Spring Boot application. We can do so as follows:

 
The start-class property is a simple way to specify the main class. However, it requires us to use the spring-boot-starter-parent as the parent pom in Maven. If we’re not using the spring-boot-starter-parent, then we can apply the start-class property to the mainClass configuration parameter of the spring-boot-maven-plugin.

2. Using mainClass Configuration

Another way of specifying the main class in a Spring Boot application is by using the mainClass configuration. This configuration can be defined in the pom.xml file of the Maven project, and it tells the spring-boot-maven-plugin which class is the main class of the application. For example, let’s say we have a main class called com.example.HelloWorldApplication, we can specify it as the mainClass of our Spring Boot application as follows:

 
The mainClass configuration is useful when we want to specify the main class without using the spring-boot-starter-parent, or when we want to override the start-class property.

3. Gradle projects

For Gradle projects, the main class can be configured explicitly using the task’s mainClassName property:

 
Alternatively, the main class name can be configured project-wide using the mainClassName property of the Spring Boot DSL:

4. Using PropertiesLauncher

Another way of specifying the main class in a Spring Boot application is by using the PropertiesLauncher. This is a special launcher that allows us to override the logical main class by using a JVM argument called loader.main. This way, we can specify the main class at runtime, without modifying the JAR or WAR file. For example, let’s say we have a main class called com.example.ExampleApplication, and we want to specify it as the main class of our Spring Boot application. We can do so as follows:

The PropertiesLauncher is useful when we want to specify the main class at runtime, or when we want to run multiple main classes from the same JAR or WAR file. However, it also has some limitations. For instance, it requires us to use the ZIP layout for the JAR or WAR file, and it may not work well with other launchers or customizations.

 
Reference: Spring Boot Gradle Plugin Reference Guide

That’s all about specifying the main class in a Spring Boot.