How to add a manifest into a Jar file

In Java, you can use manifest file to define application’s entry point, adding classpath or package version for a JAR file. In this short tutorial , we will show you how to add a custom manifest file into a Jar file.

1. Project Structure

Assume this is your project folder structure


/workspace/test/classes/com/mkyong/awt/AwtExample.class
/workspace/test/classes/manifest.txt

2. Jar It

Use below command to create a Jar file and add your custom manifest file (manifest.txt) into it.


jar -cvfm example.jar manifest.txt com/mkyong/awt/*.class

Output


$ jar -cvfm example.jar manifest.txt com/mkyong/awt/*.class
added manifest
adding: com/mkyong/awt/AwtExample$1.class(in = 638) (out= 388)(deflated 39%)
adding: com/mkyong/awt/AwtExample.class(in = 879) (out= 540)(deflated 38%)
mkyong@laptop:~/workspace/JavaTips/bin$ 

3. Done

A new “example.jar” jar file with a custom manifest file.


$ jar tf example.jar 
META-INF/
META-INF/MANIFEST.MF
com/mkyong/awt/AwtExample$1.class
com/mkyong/awt/AwtExample.class
mkyong@laptop:~/workspace/JavaTips/bin$ 

Explanation

option “m” is means include your custom manifest file. You should always careful about the order of the options, the letters “m” and “f” must appear in the same order that “manifest” and “jarfile” appear.

For example,
Correct statement
“fm” should match with “example.jar manifest.txt”


 jar -cvfm example.jar manifest.txt com/mkyong/awt/*.class

“mf” should match with “manifest.txt example.jar”


 jar -cvmf manifest.txt example.jar com/mkyong/awt/*.class

Wrong statement

 
jar -cvfm manifest.txt example.jar com/mkyong/awt/*.class

This will not work, system treats your manifest file is “example.jar”, and causing “invalid header field” error.

Reference

  1. Manifest file reference guide
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
fds
Guest
fds

my manifest is not injected, it is still the default values

LB
Guest
LB

Hi, thanks for this article, however I’m struggling to define the entry point for my MVN project.

how to add a manifest in my maven project as I have tried to build the jar file by Run As -> maven build then the jar file output you find it \workspace\CSS01\target\CSS01-0.0.1-SNAPSHOT-shaded.jar but it failed to identify the manifest.txt I added in CSS01/src/test/java. Note my java classes are sitting in CSS01/src/test/java/absa/testng/cls.

trackback
How to make an executable JAR file
Narendra
Guest
Narendra

Hello sir, the information provided in this post works fine with a single class, but can you tell me how? if i want to add more than one classes from different packages into manifest file, so that i can make an executable jar file for the entire project.

trackback
Manifest File « Namal Ubhayawardana's Blog
trackback
Jar manifest error – java.io.IOException: invalid header field | Java

[…] Please read this article about the correct way to add manifest into your Jar file. […]

trackback
The Java Archive Tool (JAR) Examples | Java

[…] More details… […]