(1) Introduction
After we use the jar command to pack the package, we will create a META-INF directory under the root directory, there will be some descriptions of the jar package information, there will certainly be a manifest. mf file, which contains the version, creator, and Class search path of the jar package. Of course, if it is an executable jar package, it will contain the main-class attribute, indicates the main method entry. The content of manifest. MF in httpclient. jar is as follows:
Manifest-version: 1.0
Implementation-title: httpcomponents Apache httpclient
Implementation-version: 4.3.1
Built-by: Oleg
Specification-vendor: the Apache Software Foundation
Created-by: Apache Maven 3.0.5
URL: http://hc.apache.org/httpcomponents-client
X-compile-source-JDK: 1.5
Implementation-vendor: the Apache Software Foundation
Implementation-vendor-ID: org. Apache
Build-JDK: 1.7.0 _ 21
Specification-title: httpcomponents Apache httpclient
Specification-version: 4.3.1
Implementation-Build: tags/4.3.1-RC2/[email protected]; 2013-10-03 2
1:09:33 + 0200
X-compile-target-JDK: 1.5
Archiver-version: plexus archiver
(2) format rules
- Basic Format attribute name: (Space) attribute value;
- Each line can contain a maximum of 72 characters. The line feed must start with a space;
- The file must end with a blank line;
- The current path of class-path is the directory where the jar package is located. to reference the jar package in the next subdirectory of the current directory, use the following format subdirectory/jar package name subdirectory/jar name, note that multiple jar packages are separated by spaces. The path Delimiter is/on any platform /;
(3) content classification
If we classify the configuration information in manifest, We can summarize the following categories:
(1)General Attributes
- Manifest-version is used to define the version of the manifest file, for example, manifest-version: 1.0.
- Created-by indicates the creator of the file. Generally, this attribute is generated by the jar command line tool, for example, created-by: Apache ant 1.5.3.
- Signature-version defines the signature version of the JAR File
- The class-path application or Class Loader uses this value to construct an internal class search path.
(2) Application-related attributes
- Main-class defines the entry class of the JAR file, which must be an executable class. Once this attribute is defined, the JAR file can be run through Java-jar XXX. jar.
(3)Package extended attributes
- Implementation-title defines the title of the extension implementation
- Implementation-version defines the version of the extension implementation
- Implementation-vendor defines the organization of the extension implementation
- Implementation-vendor-ID defines the identifier of the organization for the extension implementation
- Implementation-URL defines the URL of the extension package)
- Specification-title defines the title of the extension Specification
- Specification-version defines the version of the extension Specification
- Specification-Vendor declares the organization that maintains the Specification
- Sealed defines whether to seal the JAR file. The value can be true or false (which I don't quite understand)
(4)Applet(Applet)Related attributes
- Extendsion-list this attribute specifies the extended information list required by the applet. Each name in the list corresponds to the following attributes:
- <Extension>-extension-name defines the unique identifier of the JAR file.
- <Extension>-specification-version defines the version of the extension specification.
- <Extension>-implementation-version defines the version of the extension implementation.
- <Extension>-implementation-vendor-ID defines the supplier version number of the extension implementation.
- <Extension>-implementation-URL the latest version of the JAR File
(5) extended identity attributes
- Extension-name this property defines the unique identifier of the JAR File
(6) signature-related attributes
For signature attributes, refer to the section in mail. Jar provided by javamail.
Name: javax/mail/address. Class
Digest-algorithms: Sha MD5
Sha-Digest: ajr7rqnn // cdygouxbd06msvfi4 =
MD5-Digest: zntiq2aqatsniowxi1pqpw =
This section defines the Class Name of the class signature, the algorithm name for calculating the digest, and the corresponding digest content (encoding using the base method)
(7)Custom Attributes
In addition to the attributes mentioned above, you can also add your own attributes and response values in manifest. MF. For example, the jar package of the Java program may contain the following information:
Microedition-configuration: CLDC-1.0
MIDlet-Name: j2me_mobber MIDlet suite
MIDlet-Info-URL: http://www.javayou.com
MIDlet-icon:/icon.png
MIDlet-vendor: MIDlet suite vendor
MIDlet-1: mobber,/icon.png, mobber
MIDlet-version: 1.0.0
Microedition-profile: MIDP-1.0
MIDlet-Description: Communicator
(4) manifest. MFInformation Acquisition
Now we want to obtain information about the manifest. MF file in ant. Jar. We can use the java. util. Jar class library to obtain the information. The Code is as follows:
Java code
- Package me. simplecd;
- Import java. Io. file;
- Import java. util. Map;
- Import java. util. Jar. attributes;
- Import java. util. Jar. jarfile;
- Import java. util. Jar. manifest;
- Publicclass manifestutil {
- Publicstaticvoid main (string [] ARGs) throws exception {
- Jarfile jar = new jarfile (new file ("F: \ workspace \ simplecd \ webcontent \ WEB-INF \ Lib \ ant. Jar "));
- Manifest manifest = jar. getmanifest ();
- Attributes mainattributes = manifest. getmainattributes ();
- For (map. entry <object, Object> attrentry: mainattributes. entryset () {system. out. println ("Main \ t" + attrentry. getkey () + "-->" + attrentry. getvalue ());
- }
- Map <string, attributes> entries = manifest. getentries ();
- For (Map. Entry <string, attributes> entry: entries. entryset ()){
- Attributes values = entry. getvalue ();
- For (Map. Entry <object, Object> attrentry: values. entryset ()){
- System. Out. println (attrentry. getkey () + "-->" + attrentry. getvalue ());
- }
- }
- }
- }
The content of the manifest. MF file under ant. jar is as follows:
Manifest-version: 1.0
Ant-version: Apache ant 1.9.2
Created-by: 1.6.0 _ 27-b27 (Sun Microsystems Inc .)
Main-class: org. Apache. Tools. Ant. Main
Name: ORG/Apache/tools/ANT/
Extension-Name: org. Apache. Tools. ant
Specification-title: Apache ant
Specification-version: 1.9.2
Specification-vendor: Apache Software Foundation
Implementation-title: org. Apache. Tools. ant
Implementation-version: 1.9.2
Implementation-vendor: Apache Software Foundation
The result of running the above Code is as follows:
Main ant-version --> Apache ant 1.9.2
Main manifest-version -- & gt; 1.0
Main created-by --> 1.6.0 _ 27-b27 (Sun Microsystems Inc .)
Main main-class --> org. Apache. Tools. Ant. Main
Implementation-vendor --> Apache Software Foundation
Implementation-title --> org. Apache. Tools. ant
Specification-title --> Apache ant
Implementation-version --> 1.9.2
Specification-version --> 1.9.2
Extension-name --> org. Apache. Tools. ant
Specification-vendor --> Apache Software Foundation
Obviously, the property above the empty line is obtained through the getmainattributes method in the manifest class (the red part of the running result), which is the main information in the file, in addition, it is obtained through getentries (the blue part in the running result ).