Jar package details and META-INF Functions

Source: Internet
Author: User
Tags zip extension

How to compile a Java program into a. EXE file. There are usually only two answers. One is to create an executable JAR file package, and then it can be like. CHM
Double-click to run the file, and use jet to compile the file. However, jet needs to pay for it, and it is said that jet does not take all Java
If the program is compiled into an execution file, the performance will be discounted. Therefore, the best choice is to create an executable JAR file package. Besides, it can maintain Java's cross-platform features.

Let's take a look at what is a jar package:

1. jar package

Jar files are Java archive files, which are closely related to Java and are a document format of Java. Jar
A file is very similar to a zip file-to be precise, it is a zip file, so it is called a file package. The only difference between a jar file and a zip file is that
The content of the file contains a META-INF/manifest. MF file, which is in
File. For example, if we have files with the following directory structure:


'-- Test

'-- Test. Class

Compress it into a zip file test.zip. The internal directory structure of the ZIP file is as follows:


'-- Test

'-- Test. Class

If we use the JDK jar command to compress it into the jar package test. jar, the internal directory structure of the JAR file is:

Test. Jar


| '-- Manifest. MF

'-- Test

'-- Test. Class

2. Create an executable jar package

Creating an executable jar package to publish your program is the most typical usage of the jar package.

A Java program is composed of several. class files. These. Class
Files must be stored in different directories according to their packages. Before running, you must specify the root directory of all used packages to the classpath environment variables or Java commands.
-CP parameter. You must go to the console to run the program using Java commands. If you need to double-click the program to run the program, you must write a Windows batch file (. BAT) or
Linux Shell program. Therefore, many people say that Java is a programming language that facilitates developers to suffer from users' difficulties.

Otherwise, if the developer can make an executable jar package and hand it to the user, it will be convenient for the user to use. Install JRE (Java
Runtime Environment), the installation file will map the. jar file to javaw.exe to open. Then, for an executable jar
File package, you only need to double-click it to run the program, as convenient as reading the. chm document (. chm document is by default hh.exe
Open ). Now, the key is how to create the executable jar package.

To create an executable JAR file package, you must use the jar command with the cvfm parameter. The command is as follows:

Jar cvfm test. Jar manifest. MF Test

Here, the test. jar and manifest. MF files correspond to the F and M parameters, respectively.
Manifest. MF. It is not enough to specify a manifest. MF file because manifest is
Jar package features, executable jar package and unexecutable jar package contain manifest. The key lies in the executable JAR file package
Manifest, which contains the main-class item. The writing format in manifest is as follows:

Main-class: full name of the executable main class (including package name)

For example, assume that test. class belongs to the test package and is an executable class (defines the public static void main (string []) method), then this manifest. MF can be edited as follows:

Main-class: Test. Test <press enter>;

This manifest. MF can be put anywhere or another file name. You only need a line of main-class: Test. Test and the line ends with a carriage return. After the manifest. MF file is created, our directory structure becomes:


| -- Test

| '-- Test. Class

'-- Manifest. MF

At this time, you need to use the jar command in the upper-level directory of the test directory to create a jar file package. That is, in the directory indicated by "=" in the directory tree, use the following command:

Jar cvfm test. Jar manifest. MF Test

Then, test. jar is created in the "=" directory. This test. jar is the JAR file package that is executed. You only need to run the Java-jar test. Jar command.

Note that the created jar package must contain a complete directory structure that corresponds to the Java program package structure, just as in the preceding example. While main-class
The specified class must also be a complete class name that contains the package path, such as Test. Test in the preceding example. You can use Java
<Class Name>; to run this class, that is, in the above example, Java Test. Test can run correctly (of course, in the classpath

3. Jar command details

Jar is installed with JDK. in the bin directory under the JDK installation directory, the file name in Windows is jar.exe, and in Linux is
Jar. To run the SDK, you must use the tools. jar file in the lib directory under the JDK installation directory. However, we do not need to do anything except install JDK, because
Sun has helped us. We don't even need to put tools. jar in classpath.

We can see the usage of the jar command without any jar command is as follows:

Jar {ctxu} [vfm0m] [Jar-file] [manifest-file] [-C Directory] File Name...

{Ctxu} is a sub-command of the jar command. Each jar command can contain only one of the ctxu commands, which indicate:

-C: Create a New jar package.

-T list the contents of the jar package

-X expand the specified file or all files in the jar package.

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

Options in [vfm0m] can be selected or not. They are the option parameters of the jar command.

-V: generate a detailed report and print it to the standard output.

-F specifies the JAR file name. This parameter is usually required.

-M: Specifies the manifest file to be included.

-0: It is only stored and not compressed. the JAR file package generated in this way is larger than the JAR file package generated without this parameter, but faster.

-M does not generate the manifest file for all items. This parameter ignores the-M parameter.

A [Jar-file] is a jar package that needs to be generated, viewed, updated, or unwrapped. It is a subsidiary parameter of the-F parameter.

[Manifest-file] is the manifest configuration file, which is a subsidiary parameter of the-M parameter.

The [-C Directory] indicates the operation to run the jar command in the specified directory. It is equivalent to converting to this directory using the CD command and then executing the jar command without the-C parameter. It can only be used when creating and updating the JAR file package.

File Name... specifies a file/directory list. These files/directories are the files/directories to be added to the jar package. If a directory is specified, all files and subdirectories in the directory are automatically packaged into the package.

Here are some examples to illustrate how to use the jar command:

1) jar CF test. jar test

The command does not display the execution process. The execution result is that the test. jar file is generated in the current directory. If the current directory already has test. jar, the file will be overwritten.

2) jar CVF test. jar test

The command is the same as the result in the preceding example. However, the packaging process is shown as follows due to the function of the V parameter:


Added: Test/(read = 0) (write = 0) (0% is stored)

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

3) jar cvfm test. jar test

The command is similar to 2), but the generated test. jar does not contain the META-INF/manifest file, and the packaging process information is slightly different:

Added: Test/(read = 0) (write = 0) (0% is stored)

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

4) jar cvfm test. Jar manifest. MF Test

The running result is similar to 2), and the display information is the same, but the META-INF/manifest content in the jar package is different, it contains the contents of manifest. MF

5) jar TF test. Jar

If test. Jar already exists, you can view the content in test. Jar. For example, for test. Jar generated in 2) and 3), the command is as follows;

For 2)


META-INF/manifest. MF


Test/test. Class

For 3)


Test/test. Class

6) jar tvf test. Jar

In addition to the content shown in (5), it also includes the details of the files in the package, such:

0 wed Jun 19 15:39:06 GMT 2002 META-INF/

86 wed Jun 19 15:39:06 GMT 2002 META-INF/manifest. MF

0 wed Jun 19 15:33:04 GMT 2002 test/

7 wed Jun 19 15:33:04 GMT 2002 test/test. Class

7) jar XF test. Jar

Unpack test. jar to the current directory without any information. For 2) the generated test. Jar directory structure is as follows:



| '-- Manifest

'-- Test

'-- Test. Class

8) jar xvf test. Jar

The running result is the same as 7). Detailed information about the decompression process is displayed, for example:

Create: META-INF/

Expand: META-INF/manifest. MF

Create: Test/

Expand: Test/test. Class

9) jar UF test. Jar manifest. MF

The file manifest. mf is added to test. Jar. You can use jar TF to view test. jar.
A manifest is more than the original one. By the way, if you use the-M parameter and specify the manifest. MF file, then manifest. MF
Is used as the manifest of the configuration file, and its content will be added to the manifest; however, if it is added as a general file to the jar
In a package, it is similar to a common file.

10) jar UVF test. Jar manifest. MF

(9) The results are the same and detailed information is displayed, for example:

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

4. Some tips on jar packages

1) Use unzip to decompress the JAR File

As mentioned before, the JAR file is actually a zip file, so you can use common tools to decompress the JAR file.
Files, such as Windows WinZip, WinRAR, and Linux unzip. Use WinZip and WinRAR
And so on. The unzip is used because the-D parameter can be used to specify the target directory during decompression.

When extracting a jar file, you cannot use the-C parameter of jar to specify the extraction target, because-C
Parameters are only available when a package is created or updated. To decompress the file to a specified directory, you must first
Copy the file to the target directory and decompress it. If you use unzip, you don't need to worry about it. You just need to specify a-d parameter. For example:

Unzip test. jar-d dest/

2) use tools such as WinZip or WinRAR to create jar files

The jar file mentioned above is the ZIP file that contains the META-INF/manifest, so you only need to use WinZip, WinRAR
And then add a META-INF directory containing the manifest file to the zip package. For
When the-M parameter of the jar command specifies the configuration file, you only need to modify the manifest as needed.

3) use the jar command to create a zip file

In some Linux systems, the unzip command is provided, but there is no zip command. You can decompress the ZIP file to create a zip file.
File. To create a zip file, use the jar command with the-M parameter, because the-M parameter indicates that no manifest is added when creating the jar package.
Configuration, you only need to change the. Jar extension to the. Zip extension where the target JAR file is specified.
File, such as 3rd in the previous section:

Jar cvfm test.zip Test

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.