[Go]-jar file package and jar command in detail (MANIFEST. Use of MF)

Source: Internet
Author: User
Tags zip extension

It is often seen on the internet that someone asks how to compile the Java program into an. exe file. There are usually only two answers, one is to make an executable JAR file package, and then it can be like. The CHM document is double-clicked, and the other uses JET to compile. But jet is buying it, and it is said that jet is not able to compile all of the Java programs into executable files, and performance has to be discounted. So, the best way to make the executable JAR file package is to keep the Java cross-platform feature.
Here's a look at what a JAR file package looks like:

1. JAR File Package

The JAR file is Java Archive file, as implies, its application is related to Java, is a Java document format. JAR files are similar to ZIP files?? To be exact, it is a ZIP file, so call it a file package. The only difference between a jar file and a ZIP file is that it contains a meta-inf/manifest in the contents of the jar file. MF file, this file is created automatically when the JAR file is generated. For example, if we have some files with the following directory structure:




Compress it into a zip file test.zip, the internal directory structure of this zip file is:




If we use the JDK jar command to call it a jar package Test.jar, the internal directory structure of this jar file is:






2. Create an executable JAR file package

Creating an executable jar file package to publish your program is the most typical use of jar packages.

A Java program is made up of several. class files. These. class files must be classified according to the package they belong to, and the root directory of all the packages used should be assigned to the CLASSPATH environment variable or the-CP parameter of the Java command, and the runtime will go to the console and run with the Java command, if you need to double-click the operation The line must write the Windows batch file (. bat) or the Linux Shell program. As a result, many people say that Java is a user-friendly programming language for developers.

In fact, if the developer can make an executable JAR file package to the user, then the user is convenient to use. When installing the JRE (Java Runtime Environment) under Windows, the installation file maps the. jar file to Javaw.exe open. Then, for an executable JAR package, the user can simply double-click on it to run the program, as easily as reading the. chm document (. CHM documents are opened by default by hh.exe). The key now, then, is how to create this executable JAR file package.

To create the executable jar package, you need to use the jar command with the CVFM parameter, as an example of the test directory above, the command is as follows:

Jar CVFM Test.jar MANIFEST.MF test

Here Test.jar and manifest.mf two files, respectively, corresponding to the parameters F and M, whose plays are in MANIFEST.MF. Because to create an executable jar package, it is not sufficient to specify a MANIFEST.MF file, because manifest is a feature of the jar package, both the executable jar package and the non-executable jar package contain manifest. The key is to execute the MANIFEST of the JAR file package, which contains the Main-class. This is written in MANIFEST in the following format:

Main-class: Executable main class full name (including package name)

For example, assuming that the test.class in the previous example belongs to the Test package and is an executable class (which defines the public static void Main (string[]) method, then this MANIFEST.MF can be edited as follows:

Main-class:test. Test < Enter >

This manifest.mf can be placed in any location, or it can be another file name, only need to have main-class:test. Test line, and the line ends with a carriage return character. After creating the MANIFEST.MF file, our directory structure becomes:



| '--Test.class


At this point, you need to go to the parent directory of the test directory to use the JAR command to create the jar file package. That is, in the directory tree using the "= =" In that directory, use the following command:

Jar CVFM Test.jar MANIFEST.MF test

The Test.jar is then created in the "= =" directory, and this test.jar is the jar file package that is executed. The runtime only needs to use the Java-jar test.jar command.

It is important to note that the JAR package created needs to contain the complete directory structure corresponding to the Java program's package structure, as in the previous example. The class specified by Main-class must also be a complete class name that contains the package path, as in the previous example, test. Test, and you can use Java < class name > to run this class without hitting the JAR package, which is the Java test in the previous example. Test can be run correctly (of course, in the CLASSPATH correct case).

3. Detailed jar command

The jar is installed with the JDK, and in the Bin directory under the JDK installation directory, the file name is jar.exe,linux under Windows file named Jar. It is required to run the Tools.jar file in the Lib directory under the JDK installation directory. But we don't have to do anything but install the JDK, because SUN has done it for us. We don't even need to put Tools.jar in CLASSPATH.

Using the jar command without any, we can see the use of the Jar command as follows:

jar {ctxu}[vfm0m] [jar-file] [manifest-file] [-C directory] File name ...

where {CTXU} is a subcommand of the jar command, each time the jar command can contain only one of the CTXU, each of which represents:

-C Create a new JAR file package

-t lists the contents list of the JAR file package

-X expands the specified file or all files of the JAR package

-U update the existing jar package (add file to jar file package)

Options in [vfm0m] can be either optional or optional, they are option parameters for the jar command

-V Generate detailed reports and print to standard output

-f Specifies the JAR file name, usually this parameter is required

-m Specifies the MANIFEST manifest file that needs to be included

-0 storage, no compression, so that the resulting JAR package will be larger than the size without the parameter, but faster

-M does not produce a manifest for all items (the Manifest) file, this parameter ignores the-m parameter

[jar-file] is a jar package that needs to be generated, viewed, updated, or unpacked, which is a subordinate parameter to the-f parameter

[manifest-file] is the manifest manifest file, which is a dependent parameter of the-m parameter

The [-C directory] represents the action that goes to the specified directory to execute the jar command. It is equivalent to using the CD command to go to the directory and then execute the JAR command without the-c parameter, which can only be used when creating and updating jar packages.

Filename... Specifies a list of files/directories that are files/directories to add to the JAR package. If a directory is specified, the jar command automatically packages all files and subdirectories in the directory when it is packaged.

Here are some examples to illustrate the use of JAR commands:

1) Jar CF Test.jar Test

The command does not show the execution of the procedure, and the result is that the Test.jar file is generated in the current directory. If the current directory already exists Test.jar, then the file will be overwritten.

2) Jar CVF Test.jar test

The command is the same as in the previous example, but as a result of the V parameter, the packaging process is shown as follows:

Mark List (manifest)

Added: test/(read in = 0) (write = 0) (0% stored)

Added: Test/test.class (read in = 7) (write = 6) (14% compressed)

3) Jar CVFM Test.jar test

This command is similar to the 2 result, but the Meta-inf/manifest file is not included in the generated Test.jar, and the information for the packaging process is slightly different:

Added: test/(read in = 0) (write = 0) (0% stored)

Added: Test/test.class (read in = 7) (write = 6) (14% compressed)

4) Jar CVFM Test.jar MANIFEST.MF test

The result is similar to 2, and the display information is the same, except that the meta-inf/manifest content in the JAR package is different and contains the contents of the MANIFEST.MF.

5) Jar TF Test.jar

In the case where Test.jar already exists, you can view the contents of Test.jar, such as for 2 and 3), and the resulting test.jar should be this command respectively, the result is as follows;

for 2)


Meta-inf/manifest. Mf



for 3)



6) Jar TVF Test.jar

In addition to the content shown in Display 5), also includes details about the files in the package, such as:

0 Wed June 15:39:06 GMT 2002 meta-inf/

Wed June 15:39:06 GMT 2002 meta-inf/manifest. Mf

0 Wed June 15:33:04 GMT 2002 test/

7 Wed June 15:33:04 GMT 2002 Test/test.class

7) Jar XF Test.jar

Unpack the Test.jar to the current directory, do not display any information, for 2) generated Test.jar, after the unpacked directory structure is as follows:






Jar XVF Test.jar

The result of the operation is the same as 7, with detailed information displayed for the decompression process, such as:

Created: meta-inf/

Expand: Meta-inf/manifest. Mf

Created: test/

Expand: Test/test.class

9) jar UF Test.jar MANIFEST.MF

Added the file manifest.mf in Test.jar, this use jar TF to see Test.jar can find Test.jar more than the original one manifest. Incidentally, if you use the-m parameter and specify the MANIFEST.MF file, the MANIFEST.MF is used as the manifest file manifest, and its contents are added to the manifest, but if it is added to the JAR package as a generic file, it follows General documents.


and 9) with the same results, with detailed information such as:

Added: MANIFEST.MF (read in = 17) (write = 19) (Compressed-11%)

4. Some tips on JAR packages

1) Use Unzip to extract the JAR file

In the introduction of the jar file has been said that the jar file is actually a zip file, so you can use some of the common unzip the zip file tool to extract JAR files, such as WinZip under Windows, WinRAR, and Linux under the unzip and so on. Using WINZIP and WinRAR to decompress is because they are more intuitive and convenient decompression. With unzip, you can use the-d parameter to specify the target directory when extracting it.

When extracting a jar file, it is not possible to use the-c parameter of the jar to specify the target of the decompression, because the-c parameter is only available when the package is created or updated. Then you need to extract the files into a specified directory, you need to first copy the JAR file to the target directory, and then decompression, more trouble. If you use unzip, you don't need to be so troublesome, just specify a-D parameter. Such as:

Unzip test.jar-d dest/

2) Create JAR files with tools such as WinZip or WinRAR

The above mentioned JAR file is a zip file containing meta-inf/manifest, so only need to use WINZIP, WinRAR and other tools to create the required zip compression package, and then add a MANIFEST file in this zip package META -inf directory. If you specify the manifest file with the-m parameter of the jar command, you only need to modify the MANIFEST as needed.

3) Create a ZIP file using the jar command

Some Linux provide the unzip command, but no zip command, so you need to be able to unzip the zip file, that is, you cannot create a zip file. To create a ZIP file, use the jar command with the-m parameter, because the-m parameter indicates that the MANIFEST manifest is not added when the jar is made, then you only need to change the. jar extension to the. ZIP extension where the target jar file is specified, creating a No-compromise ZIP file, as in the previous section of the 3rd) a few changes to the example:

Jar CVFM Test.zip Test

[Go]-jar file package and jar command in detail (MANIFEST. Use of MF)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.