I just announced the new Spring 5 modules in REST With Spring:


1. Overview

This quick tutorial provides different ways of defining an entry point into a Spring Boot application via Maven and Gradle.

A Spring Boot application’s main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn’t explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

Unlike in conventional Java applications, the main class discussed in this tutorial does not appear as the Main-Class metadata property in META-INF/MANIFEST.MF of the resulting JAR or WAR file.

Spring Boot expects the artifact’s Main-Class metadata property to be set to org.springframework.boot.loader.JarLauncher (or WarLauncher) which means that passing our main class directly to the java command line won’t start our Spring Boot application correctly.

An example manifest looks like this:

Manifest-Version: 1.0
Start-Class: org.baeldung.DemoApplication
Main-Class: org.springframework.boot.loader.JarLauncher

Instead, we need to define the Start-Class property in the manifest which is evaluated by JarLauncher to start the application.

Let’s see how we can control this property using Maven and Gradle.

2. Maven

The main class can be defined as a start-class element in the pom.xml‘s properties section:

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

Note that this property will only be evaluated if we also add the spring-boot-starter-parent as <parent> in our pom.xml.

Alternatively, the main class can be defined as the mainClass element of the spring-boot-maven-plugin in the plugin section of our pom.xml:


An example of this Maven configuration can be found over on GitHub.

3. Gradle

If we’re using the Spring Boot Gradle plugin, there are a few configurations inherited from org.springframework.boot where we could specify our main class.

In the project’s Gradle file, mainClassName can be defined within springBoot configuration block. This change made here is picked up by bootRun and bootJar task:

springBoot {
    mainClassName = 'org.baeldung.DemoApplication'

Alternatively, the main class can be defined as the mainClassName property of bootJar Gradle task:

bootJar {
    mainClassName = 'org.baeldung.DemoApplication'

Or as a manifest attribute of the bootJar task:

bootJar {
    manifest {
	attributes 'Start-Class': 'org.baeldung.DemoApplication'

Note that the main class specified in the bootJar configuration block only affects the JAR that the task itself produces. The change doesn’t affect the behavior of other Spring Boot Gradle tasks such as bootRun.

As a bonus, if the Gradle application plugin is applied to the project, mainClassName can be defined as a global property:

mainClassName = 'org.baeldung.DemoApplication'

We can find an example of these Gradle configurations over on GitHub.

4. Using CLI

We can also specify a main class via the command line interface.

Spring Boot’s org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:

java -cp bootApp.jar -Dloader.main=org.baeldung.DemoApplication org.springframework.boot.loader.PropertiesLauncher

5. Conclusion

There are more than a few ways to specify the entry point to a Spring Boot application. It’s important to know that all these configurations are just different ways to modify the manifest of a JAR or WAR file.

Working code examples can be found here and here.

I just announced the new Spring 5 modules in REST With Spring:


Leave a Reply

Be the First to Comment!

Notify of