Jar command details

Source: Internet
Author: User
Tags zip extension

I often see people asking on the Internet: how to compile a Java program
. EXE file. Generally, there are only two answers. One is to create an executable JAR file package, and then double-click it to run it like the. chm file. The other is to use jet
To compile. 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
A file is a Java archive file, which is closely related to Java and a document format of Java. Jar files are very similar
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 JAR file contains
META-INF/manifest. MF file, which is automatically created when the JAR file is generated. 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.zip

'-- 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

| -- META-INF

| '-- 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.

Java
A program consists 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-CP of the classpath environment variable or Java command.
Parameter. You must go to the console to run the program using the Java command. If you need to double-click to run the program, you must write the 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 developers can create
Make it easy for you to use it as an executable jar package. Install JRE (Java Runtime
The Installation File maps the. jar file to javaw.exe. 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

This
The test. jar and manifest. MF files are the corresponding parameters F and m, respectively. Because you want to create executable
It is not enough to specify a manifest. MF file, because manifest is a feature of the JAR file package and executable jar
Both the package and the unexecutable jar package contain manifest. The key lies in the manifest of the executable JAR file package, which contains
Main-class. 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.

Yes
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, when classpath is correct ).

3. Jar command details

Jar is installed with JDK. in the bin directory under the JDK installation directory
The file name is jar.exe, and the file name is jar in Linux. To run it, you need to use tools. jar in the lib directory under the JDK installation directory.
File. 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 into 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

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

In
The file manifest. mf is added to test. Jar. You can use jar TF to view test. jar to find 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

In
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.

Decompress a jar
The-C parameter of jar cannot be used to specify the decompression 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

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

3) use the jar command to create a zip file

Yes
Some Linux systems provide unzip commands, but there is no zip command, so you need to decompress the ZIP file, that is, you cannot create a zip file. To create
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

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.