OpenJDK 8 jar tool customizing Manifest file inside jar

Solution Verified - Updated

Environment

  • OpenJDK 8

Issue

How to add custom details, such as Manifest version, OpenJDK version, and Class Path details on the manifest file?

Resolution

The example below creates a ClientJar.jar file, which has a custom manifest file: Manifest.txt, with all details to be overwriting on the manifest file.

$ cat Manifest.txt 
Manifest-Version: 37.0
Created-By: 1.7.0_237 (Example Corp) <---------------- example
Class-Path: lib/jboss-client.jar lib/EjbPrint.jar lib/jsse.jar <------------- adding jar libraries to class path
Sealed: true <------- cannot be opened
Main-Class: StandaloneClient <--------- sets main class when executing java -jar ClientJar.jar

And to compile one can overwrite it with the option "m" as below:

$jar cvfm ClientJar.jar Manifest.txt StandaloneClient.class -C lib . wildfly-config.xml

The option below writes the class StandaloneClient as the main class in the manifest file:

$JAVA_HOME/bin/jar cvfe ClientJar.jar StandaloneClient StandaloneClient.class

Root Cause

Manifest.MF contains information about the files contained in the JAR file and when a JAR file is created a default one is crated inside META-INF folder. It contain a list key-pair of information about the build, jdk version, which application that built it:

ValueMeaning
Manifest-Version: 1.0Manifest version
Archiver-Version: Plexus ArchiverArchiver version
Created-By: Apache Maven 3.6.0Maven creation
Built-By: fdemelojWho created it
Build-Jdk: 11.0.14JDK that built it
Implementation-Title: Spring Boot Traditional SampleImplementation vendor details
Implementation-Version: 1.2.3.RELEASEImplementation vendor details
Implementation-Vendor-Id: org.springframework.bootImplementation vendor details
Implementation-Vendor: Pivotal Software, IncImplementation vendor details
Main-Class: org.springframework.boot.loader.WarLauncherSpring boot will be the main class
Start-Class: sample.traditional.SampleTraditionalApplicationSpring boot for app class
Spring-Boot-Version: 1.2.3.RELEASESpring boot example
DependenciesDeclare dependencies

For other properties that can be set on the manifest file, see Content from docs.oracle.com is not included.Java SE 8 Technotes Guides.

INDEX.LIST

There is another file: INDEX.LIST, this plays a role when classloader loads the jar file, it will reads the INDEX.LIST file and uses it to construct a hash table of mappings from file and package names to lists of jar file names. In order to find a class or a resource, the class loader queries the hashtable to find the proper jar file and then downloads if it is necessary.

$ tree
.
├── AClass.class
└── META-INF
    ├── INDEX.LIST <------
    ├── MANIFEST.MF <-----
    └── maven
        └── something

Diagnostic Steps

  1. Use the verbose option: v example: jar cvfm to see what is being added:
added manifest <-------- added manifest
adding: StandaloneClient.class(in = 1923) (out= 1078)(deflated 43%)
adding: EjbPrint.jar(in = 2595) (out= 1988)(deflated 23%)
adding: jsse.jar(in = 1917344) (out= 634716)(deflated 66%)
adding: jboss-client.jar(in = 19841869) (out= 17870289)(deflated 9%)
adding: wildfly-config.xml(in = 1766) (out= 792)(deflated 55%)
  1. Example packaging a manifest file (custom one wrote beforehand): $ jar cvfm myProj.jar manifest.txt *.*
Components
Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.