How to add a manifest file to a Java JAR file

I just needed to add a manifest file to a Java JAR file, and this command worked for me:

$ jar uvfm Hello.jar manifest.txt

This assumes:

  • My original JAR file is named Hello.jar
  • The manifest file I created is named manifest.txt

After I ran that jar command I verified that it created a META-INF/MANIFEST.MF file in my JAR file with this “list” command:

$ jar tvf Hello.jar
  2696 Mon Jul 20 20:48:42 MDT 2020 Hello$.class
   752 Mon Jul 20 20:48:42 MDT 2020 Hello.class
   690 Mon Jul 20 20:48:42 MDT 2020 Hello$delayedInit$body.class
   291 Mon Jul 20 20:55:10 MDT 2020 META-INF/MANIFEST.MF

Note that the name of your manifest file doesn’t matter. As long as you use the -m option (manifest) with the -u option (update), the jar command will put your manifest file contents in the META-INF/MANIFEST.MF file.

FWIW, these are the contents of my manifest.txt file:

Manifest-Version: 1.0
Main-Class: com.alvinalexander.Hello
Specification-Title: Hello
Specification-Version: 0.1
Specification-Vendor: default
Implementation-Title: Hello
Implementation-Version: 0.1
Implementation-Vendor: default
Implementation-Vendor-Id: default

All I really wanted to specify was the Main-Class attribute, but I found these other manifest settings in another file, so I added them to my file to see what effect they might have.

In summary, if you ever need to add a manifest file to an existing Java JAR file, I hope this example is helpful.