In the article showing how to run an application bundled in a jar file, I mentioned the simplest statement java -jar YourApp.jar if your application was included a manifest file. Otherwise, the statement isn’t working. This post, therefore, presents how to add a manifest file to a jar file using either the typical way or Maven.
As far as manifest file introduced, it is normally used to define the following things:
- Define the entry point of the Application, make the Jar executable.
- Add project dependency classpath.
Presumably we have build a class named com.itersdesktop.javatechs.Demo. The following paragraphs will take this class as an illustrated example to ease the reading.
Table of Contents
In a typical way
We want to execute the main method in the class Demo in the package com.itersdesktop.javatechs when we run the JAR file.
We first create a text file named Manifest.txt with the following contents:
Main-Class: com.itersdesktop.javatechs.Demo
[tooltip text=”Warning” gravity=”nw”]Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.[/tooltip]
We then create a JAR file named Demo.jar by entering the following command:
jar cfm Demo.jar Manifest.txt com/itersdektop/javatechs/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Created-By: 1.8.0_102 (Oracle Corporation) Main-Class: com.itersdesktop.javatechs.Demo
When you run the JAR file with the following command, the main method of Demo executes:
java -jar Demo.jar
Setting an Entry Point with the JAR Tool
The ‘e’ flag (for ‘entry point’) creates or [highlight]overrides[/highlight] the manifest’s Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to Demo :
jar cfe app.jar Demo Demo.class
You can directly invoke this application by running the following command:
java -jar Demo.jar
If the entry point class name is in a package it may use a ‘.’ (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class
With Maven
This section will help you familiarise with how to use the maven-jar-plugin to create a manifest file in Maven based Java projects, and package/add it into the final jar file. When you run the command mvn package to package project into a Jar, the following meta-inf/manifest.mf file will be generated and added into the your Jar file automatically.
Define maven-jar-plugin in pom.xml , and configure the manifest file via configuration tag. The basic syntax looks like
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <mainClass>com.itersdektop.javatechs.Demo</mainClass> </manifest> </archive> </configuration> </plugin>
[tooltip text=”Notes” gravity=”nw”]Notes: we need to create an executable jar file, so the attribute is the most important to have in these settings.[/tooltip]
In Intellij IDEA, once clicking package function, we should see the result in the Run windows as shown in the following image.

For your references, you could take a look at the pom.xml content presented at the end of the post.
<?xml version="1.0" encoding="UTF-8"?>
<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.itersdesktop.javatechs</groupId>
<artifactId>Java8Features</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>com.itersdesktop.javatechs.Java8NewFeatures.LamdasExpressions</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Hopefully you find interesting points once getting through my post. All comments are welcomed!
