Skip to content

[Solved] No main manifest attribute Solution for Jar/Maven/Gradle/Spring Boot

You’ve built a Java project and packaged a .jar file.

You are ready to run it outside your IDE, or deploy it somewhere.

When you run it using java -jar <your.jar> it says something like this:

no main manifest attribute, in /src/project/target/your-jar-1.0-SNAPSHOT.jar

How to add a Manifest file

You need to create a META-INF/MANIFEST.MF file with the following content:

Main-Class: com.mypackage.MyClass
Code language: CSS (css)

The class name com.mypackage.MyClass is the main class that has the public static void main(String[] args) entry point in your application.

If you’re using Maven

If using Maven, then you will need to add this to your POM.xml:

<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> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mypackage.MyClass</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
Code language: HTML, XML (xml)

If you’re using Gradle

Likewise, if you’re using Gradle, then use this:

plugins { id 'java' } jar { manifest { attributes ( 'Main-Class': 'com.mypackage.MyClass' ) } }
Code language: Groovy (groovy)

If you didn’t build an Executable Jar

Then you can run it like this:

java -cp app.jar com.mypackage.MyClass
Code language: CSS (css)

If all else fails: Use this Maven Single Goal

If all else has failed, then you can always use this Maven goal to get the job done:

<plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.mypackage.MyClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
Code language: HTML, XML (xml)

See also  How to Save a Python Dictionary to a File in Python
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x