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/favtuts/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/favtuts/awt/*.class
Output
$ jar -cvfm example.jar manifest.txt com/favtuts/awt/*.class
added manifest
adding: com/favtuts/awt/AwtExample$1.class(in = 638) (out= 388)(deflated 39%)
adding: com/favtuts/awt/AwtExample.class(in = 879) (out= 540)(deflated 38%)
favtuts@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/favtuts/awt/AwtExample$1.class
com/favtuts/awt/AwtExample.class
favtuts@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/favtuts/awt/*.class
“mf” should match with “manifest.txt example.jar”
jar -cvmf manifest.txt example.jar com/favtuts/awt/*.class
Wrong statement
jar -cvfm manifest.txt example.jar com/favtuts/awt/*.class
This will not work, system treats your manifest file is “example.jar”, and causing “invalid header field” error.