Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

Whenever we encounter the “no main manifest attribute” message in a Spring Boot executable jar, it’s because we’re missing the declaration of the Main-Class metadata property from the MANIFEST.MF file, which is located under the META-INF folder.

In this short tutorial, we’ll focus on the causes of this problem and how to fix it.

Further reading:

Intro to Spring Boot Starters

A quick overview of the most common Spring Boot Starters, along with examples on how to use them in a real-world project.

Create a Fat Jar App with Spring Boot

Learn how to package a Spring Boot app into a deployable fat jar.

Running a Spring Boot App with Maven vs an Executable War/Jar

Learn the differences of running a Spring Boot App with Maven vs an Executable War/Jar.

2. When the Problem Occurs

Generally, we won’t have any problems if we take our pom from Spring Initializr. However, if we build our project manually by adding the spring-boot-starter-parent to our pom.xml, we might experience this issue. We can replicate it by trying a clean build of the jar:

$ mvn clean package

We’ll experience the error when running the jar:

$ java -jar target\spring-boot-artifacts-2.jar
no main manifest attribute, in target\spring-boot-artifacts-2.jar

In this example, the content of the MANIFEST.MF file is:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Baeldung
Build-Jdk: 11.0.13

3. Fixing With the Maven Plugin

3.1. Add the Plugin

The most common issue, in this case, is that we missed adding the spring-boot-maven-plugin declaration to our pom.xml file.

We’ll add the plugin definition to our pom.xml, with the Main-Class declaration under the plugins tag:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.baeldung.demo.DemoApplication</mainClass>
            <layout>JAR</layout>
        </configuration>
    </plugin>
</plugins>

However, this may not be enough to solve our issue. We might still receive the “no main manifest attribute” message after rebuilding and running our jar.

Let’s see what additional configurations and alternatives we have to solve this issue.

3.2. Maven Plugin Execution Goal

Let’s add the repackage goal to the spring-boot-maven-plugin declaration right after the configuration tag:

<executions>
    <execution>
        <goals>
            <goal>repackage</goal>
        </goals>
    </execution>
</executions>

3.3. Maven Properties and Inline Command Execution Goal

Alternatively, adding the property start-class to our pom.xml file’s properties tag permits more flexibility in the build process:

<properties>
    <start-class>com.baeldung.demo.DemoApplication</start-class>
</properties>

Now we need to build the jar by using the Maven inline command spring-boot:repackage execution goal:

$ mvn package spring-boot:repackage

4. Checking the MANIFEST.MF File Content

Let’s apply our solutions, build the jar, and then check the MANIFEST.MF file.

We’ll notice the presence of the Main-Class and Start-Class properties:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Baeldung
Build-Jdk: 11.0.13
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.baeldung.demo.DemoApplication
Spring-Boot-Version: 2.7.5
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

Executing the jar now, the “no main manifest attribute” message issue isn’t showing up anymore, and the application runs.

5. Conclusion

In this article, we learned how to solve the “no main manifest attribute” message when executing a Spring Boot executable jar.

We demonstrated how this problem can come from manually created pom.xml files, and how to add and configure the Spring Maven plugin to fix it.

As always, the example code is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!