[Reprint] How to create executable Java programs

Source: Internet
Author: User
Tags zip extension
How to create executable Java programs

Detailed description of executable jar packages and jar commands

I often see someone asking on the internet how to compile a Java program into a. 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.
Compile. However, jet has to pay for it, and it is said that jet has not compiled all Java programs into execution files, and the performance has to 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 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. 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.

A Java program is composed of several. class files. These. class files must be stored in different directories according to their packages. Before running the class files, you must specify the root directories of all used packages to the-cp parameters of the classpath environment variables or Java commands; when running, go to the console and run the Java command. If you need to directly double-click to run the command, you must write the Windows batch processing 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. When JRE (Java Runtime Environment) is installed in windows, the installation file maps the. jar file to javaw.exe to open. For an executable jar package, you only need to double-click it to run the program, which is as convenient as reading the. chm document (. chm document is opened by hh.exe by default ). 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, and their names are highlighted in manifest. MF. To create an executable JAR file package, specify a manifest. mf files are not enough because manifest is a feature of the jar package. Both executable jar packages and unexecutable jar packages contain manifest. The key lies in the manifest of the executable jar package, 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. The class specified by main-class must also be a complete class name containing the package path. test; and you can use Java <Class Name> to run this class before it is packaged into a jar file, that is, 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 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%)

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

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.