Spring Boot
main class
executable jar
Java
application configuration

How do I tell Spring Boot which main class to use for the executable jar?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Spring Boot applications, the main class is the entry point for launching the application. When you package your Spring Boot application as an executable JAR file, you may sometimes need to specify which main class to use if you have multiple main methods. This article will guide you through the process of instructing Spring Boot on selecting the appropriate main class for your executable JAR.

Understanding the Main Class in Spring Boot

A Spring Boot application's main class is typically annotated with @SpringBootApplication. This annotation is a convenience annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. The main class contains the public static void main(String[] args) method, which serves as the entry point for the application.

When you build your Spring Boot application as an executable JAR file using tools like Maven or Gradle, the build system automatically identifies the main class to use in the JAR's manifest file under the attribute Main-Class. However, if your project has multiple classes with main methods, you may need to specify which one should be used when building the JAR.

Specifying the Main Class in Maven

If you are using Maven as your build tool, you can specify the main class in your pom.xml file. Here's how you can do it:

  1. Add a <properties> section to your pom.xml file:
xml
   <properties>
       <start-class>com.example.demo.MainApplication</start-class>
   </properties>
  1. Ensure that the Spring Boot Maven plugin is configured correctly in your pom.xml:
xml
1   <build>
2       <plugins>
3           <plugin>
4               <groupId>org.springframework.boot</groupId>
5               <artifactId>spring-boot-maven-plugin</artifactId>
6               <configuration>
7                   <mainClass>${start-class}</mainClass>
8               </configuration>
9           </plugin>
10       </plugins>
11   </build>

This configuration sets the com.example.demo.MainApplication as the main class in the META-INF/MANIFEST.MF of your executable JAR.

Specifying the Main Class in Gradle

For a Gradle-based Spring Boot project, you specify the main class in your build.gradle file. Here's how:

  1. Set the mainClassName property in the build.gradle file:
groovy
   tasks.named('bootJar') {
       mainClassName = 'com.example.demo.MainApplication'
   }
  1. Ensure the Spring Boot plugin is applied:
groovy
   plugins {
       id 'org.springframework.boot' version '2.5.4'
   }

This configuration ensures that when you execute ./gradlew clean build, the specified main class is used in your executable JAR.

Understanding the Manifest File

The manifest file (META-INF/MANIFEST.MF) of an executable JAR contains metadata about the JAR, including the Main-Class attribute:

 
1Manifest-Version: 1.0
2Main-Class: com.example.demo.MainApplication
3Start-Class: com.example.demo.MainApplication
4Spring-Boot-Version: 2.5.4
5...
6

The Main-Class attribute specifies the class that contains the main method, which serves as the entry point for the application. The Start-Class is also used by Spring Boot to specify the application class to start.

Troubleshooting Common Issues

If your executable JAR does not run as expected, here are some common issues and how to resolve them:

  1. Incorrect Main-Class Specification:
    • Double-check the class name for typos within the pom.xml or build.gradle.
  2. Build Tool Configuration:
    • Ensure that Maven or Gradle is correctly configured with the Spring Boot plugin.
  3. Multiple Artifacts:
    • If multiple Spring Boot applications are bundled, verify no conflict in the Main-Class setting.
  4. Classpath Issues:
    • Make sure all dependencies are correctly included in the JAR.

Summary

Here's a table summarizing the steps and configurations for specifying the main class in Spring Boot projects using Maven and Gradle:

AspectMaven ConfigurationGradle Configuration
Set Main Class<start-class>com.example.demo.MainApplication</start-class>mainClassName = 'com.example.demo.MainApplication'
Build Plugin<artifactId>spring-boot-maven-plugin</artifactId>id 'org.springframework.boot' version '2.5.4'
Manifest EntryAutomatically sets Main-Class and Start-Class in MANIFEST.MFAutomatically sets Main-Class and Start-Class in MANIFEST.MF
Command to Buildmvn clean package./gradlew clean build

Specifying the main class for your Spring Boot application's executable JAR is essential when dealing with multiple entry points. By properly configuring your build tool, you streamline your application's launch process ensuring that the correct entry point is called.


Course illustration
Course illustration

All Rights Reserved.