Often I saw someone on the Internet: How to compile the Java program into .exe file. Usually answers only two, one is to make an executable JAR file package, you can double-click on the .chm document; and another answer, use Jet to compile. But Jet is to buy with money, and it is said that JET is not compiled into executables in all Java programs, and performance is also a discount. Therefore, the method of making executable JAR file packages is the best choice, let alone, it can maintain Java cross-platform feature. Let's take a look at what is a jar file package:
JAR file package
The JAR file is Java Archive File, and the appraisal, its application is closely related to Java, a document format of Java. The JAR file is very similar to zip file - accurately, it is a zip file, so it is called a file package. The only difference between the JAR file and the ZIP file is to include a meta-inf / manifest.mf file in the content of the JAR file, which is automatically created when generating a JAR file. For example, if we have some files with the following directory structure:
==
`- test
`- Test.class
Complicize it into a zip file Test.zip, the internal directory structure of this ZIP file is:
Test.zp
`- test
`- Test.class
If we use the Jar command to use Jar to put it into a JAR file Test.jar, the internal directory structure of this JAR file is:
Test.jar
| - META-INF
| `- manifest.mf
`- test
`- Test.class
2. Create an executable JAR file package
Making an executable JAR file to publish your program is the most typical usage of the JAR file package.
The Java program consists of several .class files. These .class files must be stored separately according to their own packages; need to specify all the root of all the packages used to the ClassPath environment variable or the -cp parameters of the java command before running; if you want to go to the console Use the java command to run, if you need to directly double-click Run, you must write Windows batch file (.bat) or Linux's shell program. Therefore, many people say that Java is a programming language that facilitates developers who have bitter users.
In fact, if the developer can make an executable JAR file package to the user, then the user needs to be convenient. When you install JRE (Java Runtime Environment) under Windows, the installation file will map the .jar file to Javaw.exe. Then, for an executable JAR file package, users only need to double-click it to run the program, and read. Thechm document is as convenient (.chm documentation is opened by HH.exe). Then, the key now is how to create this executable JAR file package.
Create an executable JAR file package, you need to use the JAR command with CVFM parameters, and the same TEST directory as an example, the command is as follows:
Jar Cvfm Test.jar Manifest.mf Test
Here Test.jar and Manifest.mf two files, respectively, corresponding parameters f and M, and its weight play in Manifest.mf. Because you want to create an executable JAR file package, it is not enough to deserve a manifest.mf file, because Manifest is the feature of the JAR file package, the executable JAR file package and the unforgable JAR file package contain manifest. The key is the Manifest, which can perform the JAR file package, which contains the main-class. This is written in manifest as follows: main-class: Executable main class full name (including the package name)
For example, suppose Test.class in the above example is a TEST package, and is an executable class (defined the public static void main (String []) method), then this manifest.mf can be edited as follows:
Main-class: Test.test
This manifest.mf can be placed anywhere, or other file names, only one line-class: Test.Test is one line, and the line is over the end of one. After creating the Manifest.mf file, our directory structure becomes:
==
| - Test
| `- Test.class
`- manifest.mf
At this time, you need to use the JAR command to create a JAR file package in the upper-level directory of the TEST directory. That is, in the directory represented by "==" in the directory tree, use the following command:
Jar Cvfm Test.jar Manifest.mf Test
After the "==" directory created Test.jar, this Test.jar is the executed JAR file package. Java -jar test.jar command only needs to run.
It should be noted that in the created JAR file package, it is necessary to include the full, directory structure corresponding to the package structure of the Java program, just like the above example. The class specified by Main-Class must also be complete, including the class name of the package path, such as Test.Test in the above case; and you can use Java
3. JAR command details
JAR is installed with JDK, in the bin directory in the JDK installation directory, the file name under Windows is jar.exe, and the file name under Linux is JAR. It runs to use the Tools.jar file in the Lib directory in the JDK installation directory. However, in addition to installing JDK, we don't need to do anything, because Sum has helped us do well. We don't even need to put Tools.jar in the ClassPath.
Use no jar commands that we can see that the JAR command is as follows:
Jar {ctxu} [vfm0m] [jar- file] [manifest- file] [-c directory] file name ...
Where {ctxu} is the subcommand of the jar command, each JAR command can only contain one of the CTXU, which represent:
-c Create a new JAR file package
-T lists the content list of JAR file packages
-x Expand the specified file or all files of the JAR file package
-u Update existing JAR file package (add file to the JAR file)
The options in [VFM0M] can be optionally or not, they are the option parameters of the jar command.
-v generation detailed report and prints to standard output -f specified JAR file name, usually this parameter is necessary
-m Specifies the Manifest manifest file that needs to be included.
-0 only stores, does not compress, which generated JAR file package is larger than the volume of the parameter, but the speed is faster
-M does not generate a list of all items (MANIFEST) file, this parameter ignores -m parameters
[JAR-File] The JAR file package needs to be generated, viewed, updated, or unlocked, which is the accessory parameter of the -f parameter
[Manifest- File] MANIFEST list file, it is an accessory parameter of -m parameter
[-C Directory] Indicates to the operation of the specified directory to execute this JAR command. It is equivalent to using the CD command to execute the JAR command without -c parameters in the directory, which can only be available when creating and updating the JAR file package.
File name ... Specify a file / directory list, which is to add the file / directory to the JAR file. If the directory is specified, the JAR command will automatically put all the files and subdirectories in the directory into the package.
Let's give some examples to illustrate the usage of JAR commands:
1) JAR CF TEST.JAR TEST
This command does not have the display of the execution process, the execution result is the Test.jar file in the current directory. If the current directory already has Test.jar, then the file will be overwritten.
2) JAR CVF TEST.JAR TEST
This command is the same as the result in the previous example, but due to the function of V parameters, the package process is displayed, as follows:
Mandarin list (Manifest)
Add: TEST / (read = 0) (write = 0) (0% stored)
Add: Test / Test.class (read = 7) (write = 6) (40% compressed)
3) JAR CVFM TEST.JAR TEST
This command is similar to 2), but does not contain meta-inf / manifest files in the generated Test.jar, the information of the package process is slightly different:
Add: TEST / (read = 0) (write = 0) (0% stored)
Add: Test / Test.class (read = 7) (write = 6) (40% compressed)
4) JAR CVFM Test.jar Manifest.mf Test
Running results and 2) Similar, display information is the same, just generate the meta-inf / manifest content in the JAR package, which contains the contents of 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 2) and 3), should be this command, and the results are 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 displaying the content shown in 5), it also includes detailed information in the package file, such as:
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
Unlock Test.jar to the current directory, do not display any information, for 2) generated Test.jar, the unlocked directory structure is as follows: ==
| - META-INF
| `- manifest
`- test
`- Test.class
8) JAR XVF Test.jar
The operation results are the same as 7), and there is more information for the decompression process, such as:
Create: Meta-Inf /
Expand: meta-inf / manifest.mf
Create: TEST /
Expand: Test / Test.class
9) jar uf test.jar manifest.mf
Add file manifest.mf in Test.jar, this use JAR TF to see Test.jar can find that Test.jar is more than one manifest than the original. By the way, if you use the -m parameter and specify the manifest.mf file, then manifest.mf is used as a manifest file manifest, and its content will be added to Manifest; however, if it is added as a general file to the JAR file In the package, it is not different from general documents.
10) Jar UVF Test.jar Manifest.mf
The same is the same as 9), and there is a detailed information, such as:
Add: manifest.mf (read = 17) (write = 19) (compressed -11%)
4. Some techniques about JAR file packages
1) Use unzip to extract the JAR file
When introducing the JAR file, the JAR file is actually a zip file, so you can use a common tool to extract the Zip file to extract the JAR file, such as WinD, WinRar et al. And Linux under WinDOP, etc. . Using Winzip and WinRAR, the decompression is because they decompressed more intuitive and convenient. Use unzip because it can specify the target directory using the -d parameter when it decompresses.
When decompressing a JAR file, it is not possible to use the JAR-C parameter to specify the decompressed target, because the -c parameter is only available when creating or updating the package. Then you need to extract the file to a specified directory, you need to copy this JAR file to the target directory, and then decompressed, trouble. If you use unzip, you don't need to be so troublesome, just you need to specify a -D parameter. Such as:
Unzip test.jar -d DEST /
2) Creating a JAR file using tools such as Winzip or WinRAR
The JAR file mentioned above is a ZIP file containing meta-inf / manifest, so you only need to use Winzip, WinRAR and other tools to create the ZIP compressed package, and add a Meta-inflight containing the manifest file in this ZIP compression package. Catalog. For the case of using the -m parameter using the jar command, only the manifest is required to modify this manifest.
3) Create a ZIP file using the jar command
Some Linux provide UNZIP commands, but there is no zip command, so you need to decompress the ZIP file, you can't 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 making the JAR package, then only needs to change the .jar extension in the specified target JAR file. The .zip extension, created is an unclatted ZIP file, such as the third example of the previous section): JAR CVFM Test.zip Test