How to Use the jar command correctly

Source: Internet
Author: User
Tags zip extension

Jar files are Java archive files, which are closely related to Java and are a document format of Java. Jar files are very similar to zip files-specifically, they are zip files, so they are called file packages. The only difference between a jar file and a zip file is that the content of the JAR file contains a META-INF/manifest. MF file, which is automatically created when the JAR file is generated.

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 don't need to do anything except install JDK, because Sun has already 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:

Manifest)

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/

META-INF/manifest. MF

Test/

Test/test. Class

For 3)

Test/

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:

=

| -- META-INF

| '-- 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. Use jar TF to view test. Jar. You can find that test. Jar has a manifest more than the original one. By the way, if you use the-M parameter and specify manifest. mf file, then manifest. mf is used as the manifest of the inventory file, and its content will be added to the manifest; however, if it is added to the jar package as a general file, it is no different from the general 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%)

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 for extracting ZIP files to decompress the JAR file, for example, Windows WinZip, WinRAR, and Linux unzip. Use WinZip and WinRAR to decompress the files because they are intuitive and convenient. 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 the-C parameter is only available when the package is created or updated. To decompress the file to a specified directory, You need to copy the JAR file to the target directory before decompression, Which is troublesome. 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 other tools to create the zip compressed package, add a META-INF directory containing the manifest file to the zip package. If you use the-M parameter of the jar command to specify 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. To create a zip file, use the jar command with the-M parameter, because the-M parameter indicates that the manifest list is not added when creating the jar package, in this case, you only need. change the jar extension. the zip extension is used to create a zip file without compromise. For example, change the example 3rd in the previous section:

Jar cvfm test.zip Test

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.