Using the MANIFEST.MF File in Java

I noticed that the JAR, WAR, and EAR files have a MANIFEST.MF file in the META-INF folder.

What is the use of the MANIFEST.MF file? What all things can be specified in this file?

+62
Oct 07 '12 at 10:13
source share
2 answers

The contents of the manifest file in the JAR file created with version 1.0 of the Java Development Kit are as follows.

 Manifest-Version: 1.0 

All entries are name-value pairs. The header name is separated by a colon from its value. The default metric shows that it conforms to version 1.0 of the manifest specification. The manifest may also contain information about other files that are packaged in an archive. Exactly what information about the file is written in the manifest will depend on the intended use of the JAR file. The default manifest file makes no assumptions about what information it should write about other files, so its only line contains data about itself. Special Purpose Manifest Headers

Depending on the intended role of the JAR file, you might need to change the default manifest. If the JAR file is created for archiving only, the MANIFEST.MF file has no purpose. In most cases, the use of JAR files goes beyond simple archiving and compression and requires that the manifest file contains special information. Below are brief descriptions of the headers required for some special functions of the JAR file.

Applications that make up the JAR file: If the application comes in a JAR file, the Java virtual machine should be told what the entry point to the application is. The entry point is any class with the public static void main (String [] args) method. This information is provided in the Main-Class header, which has a general view:

 Main-Class: classname 

The class name value must be replaced by the application entry point.

Download Extensions:. Extension downloads are JAR files referenced by the manifest files of other JAR files. In a typical situation, the applet will be linked in a JAR file, whose manifest refers to a JAR file (or several JAR files), which will serve as an extension for the purposes of this applet. Extensions can refer to each other in the same way. Download extensions are listed in the Class-Path header field in the manifest file of the applet, application, or other extension. The Class-Path header might look like this:

 Class-Path: servlet.jar infobus.jar acme/beans.jar 

In this header, the classes in servlet.jar, infobus.jar, and acme / beans.jar will serve as extensions for the applet or application. The URLs in the Class-Path header are relative to the URL of the applet or application JAR file.

Package compaction: A package in a JAR file can be arbitrarily sealed, which means that all classes defined in this package must be archived in a single JAR file. A package can be sealed to ensure version compatibility between your software classes or security measure. To seal a package, a Name header must be added for the package, followed by a Sealed header similar to this:

 Name: myCompany/myPackage/ Sealed: true 

The value of the Name header is the relative path to the package. Note that it ends with the '/' character to distinguish it from the file name. Any headers following the Name header, without any intermediate blank lines, refer to the file or package specified in the Name header. In the above example, since the Sealed header appears after the Name: myCompany / myPackage header, with no empty lines between them, the Sealed header will be interpreted as applying (only) to the myCompany / myPackage package.

Package Versions: The package version specification defines several manifest headers for storing version information. Each package can be assigned one set of such headers. Version headers should appear directly below the Name heading for the package. This example shows all version headers:

 Name: java/util/ Specification-Title: "Java Utility Classes" Specification-Version: "1.2" Specification-Vendor: "Sun Microsystems, Inc.". Implementation-Title: "java.util" Implementation-Version: "build57" Implementation-Vendor: "Sun Microsystems, Inc." 
+65
07 Oct '12 at 10:20
source share

Manifest.MF contains information about the files contained in the JAR file.

Whenever a JAR file is created, the manifest.mf file is created by default inside the META-INF folder and contains default entries like this:

 Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) 

These are records as "header: value" pairs. The first specifies the version of the manifest, and the second specifies the version of the JDK with which the JAR file is created.

Main class header: When the JAR file is used to bind the application in the package, we need to specify the class that serves as the entry point for the application. We provide this information using the Main-Class header of the manifest file,

Main class: {full class name}

The Main-Class value here is a class that has a main method. After specifying this entry, we can run the JAR file to run the application.

Class header: In most cases, we need to access other JAR files from classes packaged in the application JAR file. This can be done by providing your full paths in the manifest file using the "class-class header",

Path class: {jar1-name jar2-name directory-name / jar3-name}

This header can be used to specify external JAR files on the same local network, and not inside the current JAR.

Package version related headers: When a JAR file is used to control package versions, the following headers are used according to the Java language specification:

 Headers in a manifest Header | Definition ------------------------------------------------------------------- Name | The name of the specification. Specification-Title | The title of the specification. Specification-Version | The version of the specification. Specification-Vendor | The vendor of the specification. Implementation-Title | The title of the implementation. Implementation-Version | The build number of the implementation. Implementation-Vendor | The vendor of the implementation. 

Packing related seals:

We can also indicate whether any specific packages should be closed inside the JAR file, which means that all classes defined in this package should be archived in the same JAR file. This can be specified using the "Sealed Header",

Name: {package / some-package /} Sealed: true

Here, the package name must end with "/.

Increased security with manifest files:

We can use manifest file entries to ensure the security of the web application or applet that it packs with various attributes: Permissions, Codebae, Application Name, Reliability, and more.

Folder META-INF:

This folder contains the manifest file. In addition, it may contain more files containing metadata about the application. For example, in the EJB module JAR file, this folder contains the EJB deployment descriptor for the EJB module along with the manifest file for the JAR. In addition, it contains an xml file containing the mapping of abstract EJB links to the specific resources of the application server container on which it will run.

Reference:
https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

+11
Aug 15 '14 at 8:38
source share



All Articles