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:
- Add a
<properties>section to yourpom.xmlfile:
- Ensure that the Spring Boot Maven plugin is configured correctly in your
pom.xml:
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:
- Set the
mainClassNameproperty in thebuild.gradlefile:
- Ensure the Spring Boot plugin is applied:
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:
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:
- Incorrect Main-Class Specification:
- Double-check the class name for typos within the
pom.xmlorbuild.gradle.
- Build Tool Configuration:
- Ensure that Maven or Gradle is correctly configured with the Spring Boot plugin.
- Multiple Artifacts:
- If multiple Spring Boot applications are bundled, verify no conflict in the
Main-Classsetting.
- 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:
| Aspect | Maven Configuration | Gradle 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 Entry | Automatically sets Main-Class and Start-Class
in MANIFEST.MF | Automatically sets Main-Class and Start-Class
in MANIFEST.MF |
| Command to Build | mvn 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.

