Usage of jar command MANIFEST.MF

Source: Internet
Author: User

1. Enter "Jar-help" on the command line, and you can see the detailed usage of the jar in K.

2. Example:

1 Archive Two class documents into a JAR file: JAR-CVF xx.jar a.class B.class

2 Use the manifest file MANIFEST.MF to archive all the files in the Dir directory into a jar: JAR-CVFM xx.jar manifest.mf dir/*

The format of the manifest file is simple, and each row is a "name-value" corresponding:

The property name begins, followed by ":", then the property value, with a maximum of 72 characters per line, and if required, you can continue on the next line, continuing with a space, and a line beginning with a space as a continuation of the previous line.
Use the M option to pass in the manifest file for the specified file name, for example
Jar CVFM Myapplication.jar MYAPPLICATION.MF [-c]classdir

Detailed usage of MANIFEST.MF (turn)

Now let's take a look at the role of the manifest file, if we now have a Java application packaged in Myapplication.jar, main class is Com.example.myapp.MyAppMain, Then we can use the following command to run  
Java-classpath Myapplication.jar Com.example.myapp.MyAppMain
This is obviously too much trouble, Now let's create our own Manifest file, as follows:
manifest-version:1.0
Created-by:jdj example
Main-class: Com.example.myapp.MyAppMain
So that we can run the program using the following command: (obviously much simpler, and will not cause unnecessary spelling errors)
Java-jar Myapplication.jar


to manage the dependent resources of the jar
very few Java applications will have just one jar file, and other class libraries are generally required. For example, my application uses Sun's Javamail classes, and in Classpath I need to include Activation.jar and Mail.jar, so that when I run the program, we'll add some more:
Java- Classpath Mail.jar:activation.jar-jar Myapplication.jar
in different operating systems, the delimiters between jar packs are not the same, using ":" in Unix, ";" In Windows, It's also inconvenient.
Again, we rewrite our Manifest file, as follows
manifest-version:1.0
Created-by:jdj example
Main-class: Com.example.myapp.MyAppMain
Class-path:mail.jar Activation.jar
(Joined by Class-path:mail.jar Activation.jar, separate two jar packages with a space
so we can still use the same command as in the previous example to execute the program:
Java-jar Myapplication.jar
The Class-path property contains a space-delimited jar file. In these jar file names to use escape characters, such as spaces, to be represented as "%20", in the representation of the path, "/" to separate the directory (), regardless of the operating system, ( Even in window), and here is the relative path (relative to the jar file itself):  
manifest-version:1.0
Created-by:jdj example
Main-class: Com.example.myapp.MyAppMain
Class-path:ext/mail.jar Ext/activation.jar


Multiple main Classes (multi-Main class)
There is also a multiple main classes case, if your application may have command-line and GUI versions, or some different applications share many of the same code, then you may have more than one main Class, We recommend that you take the strategy of playing a shared class as a Lib package, and then playing different applications into different packages, marking the main class separately:
Manifest for Myapplicationlib.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:mail.jar Activation.jar

Manifest for Myappconsole.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:myapplicationlib.jar
Main-class:com.example.myapp.myappmain

Manifest for Myappadmin.jar:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:myapplicationlib.jar
Main-class:com.example.myapp.myadmintool
In the manifest file of Myappconsole.jar and Myappadmin.jar, separate the Main Class
Package Versioning
When the release is complete, if the user wants to know which code is who. What is the current version. What version of the class library to use. There are a lot of solutions, manifest provides a better way to describe each package in the manifest file.


Java adheres to the principle of separating implementation and description, and the package description defines what package is, and the implementation instructions define who provides the implementation of the description, describes and implements the include name, version number, and provider. To get this information, you can view the system properties of the JVM (using Java.lang.System.getProperty ())
In the manifest file, I can define the description and implementation version for each package, declare the name, and add the description attribute and implementation attributes that are
Specification-title
Specification-version
Specification-vendor
Implementation-title
Implementation-version
Implementation-vendor
Descriptive information is important when you want to provide a class library or programming interface, as shown in the following example:
manifest-version:1.0
CREATED-BY:JDJ Example
Class-path:mail.jar Activation.jar
name:com/example/myapp/
Specification-title:myapp
specification-version:2.4
Specification-vendor:example.com
Implementation-title:com.example.myapp
Implementation-version:2002-03-05-a
Implementation-vendor:example.com
Package Version Query
After adding the Package description in the manifest file, you can use the Java.lang.Package class provided by Java for Package information queries, listing 3 basic methods for obtaining the Package object
1.package.getpackages (): Returns a list of all defined Package in the system
2.package.getpackage (String PackageName): Returning Package by name
3.class.getpackage (): Returns the package of the given Class
This method can be used to dynamically obtain package information.
Note that if no class is loaded in the given package, the package object cannot be obtained

Manifest Tips
Always starts with the Manifest-version property
Maximum of 72 characters per line, if more than, use the continuation of the line
Make sure each line ends with a carriage return, or the row will be ignored
If the existing path in the Class-path, use the "/" separator directory, regardless of the platform
Separating primary and package properties with empty rows
Use "/" instead of "." To separate package and class, like com/example/myapp/.
class to end With. class, package to end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.