Partner – Trifork – NPI (cat=Spring Boot)
announcement - icon

Navigating the complexities of Spring can be difficult, even for seasoned developers.

If you need direct, practical help and guidance with your own Spring work, Trifork's CTO, Joris Kuipers, is running a closed-door call.

It's free, but it's limited to only 3 seats, so if you need it, I would join quickly and be sure to attend:

>>> CTO Spring Open Office Hour Session - Technical Guidance

With more than 15 years of leading custom software development projects involving Spring, Joris has gained a lot of real-world experience, and this call is about sharing and helping the community.


Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


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: com.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 = 'cpm.baeldung.DemoApplication'

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

bootJar {
    mainClassName = 'cpm.baeldung.DemoApplication'

Or as a manifest attribute of the bootJar task:

bootJar {
    manifest {
	attributes 'Start-Class': 'com.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 = 'com.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=com.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.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!