In this post, we will see how to solve Unable to execute jar- file: “no main manifest attribute”.
Table of Contents
Problem
When you have a self-executable jar and trying to execute it. You might get this error.
Solution
Maven
You might get this error when Main-Class
entry is missing in MANIFEST.MF file. You can put maven-jar-plugin
plugin in pom.xml
to fix it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <mainClass>org.arpit.java2blog.AppMain</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> |
org.arpit.java2blog.AppMain
is a fully qualified name of main class. You need to replace it with your application’s main class.
Run mvn clean install
and then execute the jar file as below.
1 2 3 |
java -jar AppMain-0.0.1-SNAPSHOT.jar |
AppMain-0.0.1-SNAPSHOT.jar
is application jar that you want to run.
Spring boot application
In case, you are getting an error while running spring boot application, you can solve it with spring-boot-maven-plugin
.
1 2 3 4 5 6 7 8 9 10 11 |
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> </plugin> </plugins> </build> |
Gradle
In case you are using gradle, you can solve this with following entry.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
plugins { id 'java' } jar { manifest { attributes( 'Main-Class': 'org.arpit.java2blog.AppMain' ) } } |
Please replace org.arpit.java2blog.AppMain
with your main class.
Root cause
When you run self-executable jar, java will look for the Main-Class
in MANIFEST.MF file located under META-INF folder. If it is not able to find an entry,then it will complain with Unable to execute jar- file: “no main manifest attribute”
.
MANIFEST.MF contains information about files contained in the Jar file.
1 2 3 4 5 6 |
Manifest-Version: 1.0 Built-By: Arpit Mandliya Build-Jdk: 1.8.0_101 Created-By: Maven Integration for Eclipse |
Once you run the above-mentioned solution and reopen MANIFEST.MF
file in jar again, you will see Main-Class entry.
1 2 3 4 5 6 7 |
Manifest-Version: 1.0 Built-By: Arpit Mandliya Build-Jdk: 1.8.0_101 Created-By: Maven Integration for Eclipse Main-Class: org.arpit.java2blog.AppMain |
This should solve Unable to execute jar- file: “no main manifest attribute”. Please comment in case you are still facing the issue.