[Solved] No main manifest attribute, in jar Maven and SpringBoot

The problem we are going to discuss here is about one of the scenarios where you have a self-executing JAR file having a Java application bundled inside it and you try executing it. When we are executing the JAR file, we need to point to a class with a main method ( public static void main(String[] args) which will be serving as the starting point for our application. This class is the one with the main method. In order to run the main method from our jar, we need to mention it inside the ‘Main-Class’ property of the manifest file (mainClass attribute in POM.xml in case of Mavenized application) which needs to be bundled with the code as well.

Problem:

no main manifest attribute, in "APPLICATION_NAME.jar"

For explanation purpose, we have an application named "Tutorial" with a class named Demo.java under com.javahungry package. Below are the content of the POM.xml and Demo.java file.


POM.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javahungry</groupId>
 <artifactId>Tutorial</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Tutorial</name>
</project>


Demo.java

package com.javahungry; 
public class Demo {

    public static void main(String args[]) 
    {
        System.out.println(" Java Hungry ");       
    }
}

Solution

This error occurs whenever Main-Class property is missing from manifest file MANIFEST.MF which can be found under META-INF folder in the jar.

Correct it by adding below lines to your pom.xml


<!-- include below build tag to your existing pom.xml  --> 
<build>
 <plugins>
     <plugin> 

                <!-- Building an executable jar -->

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                  <archive>
                    <manifest>

                    <!-- give full qualified name of your main class-->
                      <mainClass>com.javahungry.Demo</mainClass>

                    </manifest>
                  </archive>
                </configuration>
    </plugin>
 </plugins>
</build>


Let’s build the Tutorial application after adding the above changes in pom.xml.

Building the project in eclipse:

1. Running the application with below goal from Eclipse

Application Name -->  Right Click --> Run As --> Maven Install 

maven install 

or run below goal from command line

mvn install

This will create a jar file of the application (in this case Tutorial-0.0.1-SNAPSHOT.jar).

Execute the JAR:

2. We will now try executing this jar manually through command line and see what happens.

•    Open command prompt.
•    Go to the location of the jar file (usually set to application’s target folder in the Eclipse workspace).
•    Execute the command  java -jar .jar. See the example below.
 

java -jar Tutorial-0.0.1-SNAPSHOT.jar

The option -jar above only works when the JAR file is an executable JAR file. An executable JAR means JAR file must contain a manifest file with a Main-Class property in it.

If jar is not an executable JAR, then you need to run the program with below command

java -cp  Tutorial-0.0.1-SNAPSHOT.jar  com.javahungry.Demo

where com.javahungry.Demo is a class which contains main method to run the program.

Output :

Java Hungry

As you can see that the ‘no main manifest attribute’ related error has been resolved and our application has given us the expected output.

Fix no main manifest attribute, in jar Error in Spring Boot

Add spring-boot-maven-plugin in build section of your pom.xml. If it is not added then add the below lines

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
      </plugins>
    </build>


Explanation

As we all know there must be an entry point to any java application and it must be a Java class containing the main method. In the problem we just discussed here, the manifest file was not having Main-Class property defined in it.

This property is responsible for indicating the Java class which main method (public static void main(String[] args)) needs to be executed. In other words, it tells Java about the starting point of the application.

Let us see what happens if we do not include the Main-Class property in manifest file of our project.
Current MANIFEST.MF file contains below content.

MANIFEST.MF
Manifest-Version: 1.0
Built-By: Subham Mittal
Build-Jdk: 1.8.0_91
Created-By: Maven Integration for Eclipse


1. Running the application with below goal from Eclipse


Application Name -->  Right Click --> Run As --> Maven Install

maven install 

or run below goal from command line

mvn install

This will create a jar file of the application (in this case Tutorial-0.0.1-SNAPSHOT.jar).

2. We will now try executing this jar manually through command line and see what happens.

Execute the JAR :

1.  Open command prompt.
2.  Go to the location of the jar file (usually set to application’s target folder in the Eclipse workspace).

Execute the command  java -jar .jar. See the example below. 

java -jar Tutorial-0.0.1-SNAPSHOT.jar

Output :

no main manifest attribute, in Tutorial-0.0.1-SNAPSHOT.jar


As we can deduce that this error has occurred because we did not define any starting point for the Tutorial application while creating the Tutorial-0.0.1-SNAPSHOT.jar file. The MANIFEST.MF file that was generated by default did not contain Main-Class property in this case.

Let’s have a look at the generated default manifest file.

MANIFEST.MF
Manifest-Version: 1.0
Built-By: Subham Mittal
Build-Jdk: 1.8.0_91
Created-By: Maven Integration for Eclipse

You can see Main-Class property is missing in the above MANIFEST.MF file.

To fix this issue, we have taken help of maven-jar-plugin which provides jar building capabilities. In the newer versions of this plugin like 3.1.2, we are supposed to define our own manifest file as “useDefaultManifestFile” parameter has been removed from the plugin configuration.

We added maven-jar-plugin in pom.xml with mainClass property inside it.

<mainClass>com.javahungry.Demo</mainClass>


MANIFEST.MF (after including maven-jar-plugin and mainClass property in the pom file):

Manifest-Version: 1.0
Built-By: Subham Mittal
Build-Jdk: 1.8.0_91
Created-By: Maven Integration for Eclipse
Main-Class: com.javahungry.Demo 


Check out the highlighted "Main-Class" property that has been added to the manifest file after we have updated the pom file (refer Solution part). It indicates that the main method of com.javahungry.Demo class of our Tutorial  application will be executed. That's all for the post no main manifest attribute in jar error. Please mention in the comments if you are still facing the error or solve it by any other method.

References:
Default Manifest
Apache Maven jar plugin

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry