The manifest file contain many information about the Jar file itself, you can study the manifest file in detail here

In this tutorial , you will learn how to add your specified 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 this 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
mkyong@laptop:~/workspace/JavaTips/bin$ 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

You had created a “example.jar” jar file with your custom manifest file.

mkyong@laptop:~/workspace/JavaTips/bin$ 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$

Explantion

option “m” is mean include the manifest information from your specified manifest file. You should always caution 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
Incorrect statement
jar -cvfm manifest.txt example.jar com/mkyong/awt/*.class

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

Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~