Specify main class in a Spring Boot
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:
1 2 3 |
<properties> <start-class>com.example.HelloWorldApplication</start-class> </properties> |
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:
1 2 3 4 5 6 7 8 9 10 11 |
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.HelloWorldApplication</mainClass> </configuration> </plugin> </plugins> </build> |
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:
1 2 3 |
bootJar { mainClassName = 'com.example.ExampleApplication' } |
Alternatively, the main class name can be configured project-wide using the mainClassName
property of the Spring Boot DSL:
1 2 3 |
springBoot { mainClassName = 'com.example.ExampleApplication' } |
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:
1 2 |
java -cp bootApp.jar -Dloader.main=com.example.ExampleApplication org.springframework.boot.loader.PropertiesLauncher |
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.
[Resolved] Spring Boot app shuts down automatically at startup
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)