How to make executable Java programs

xiaoxiao2021-03-31  203

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, then you can double-click on the same .chm document; and the other is to compile using Jet. But Jet is to buy with money, and it is said that Jet is not able to compile all Java programs into executables, 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: 1. JAR file jar file is Java Archive File, as the name, its application is close 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 Complicates it into a zip file Test.zip, the internal directory structure of this ZIP file is: Test.zip` - Test `- Test.class If we use Jar command to use Jar commands Test.jar, the internal directory structure of this JAR file is: Test.jar | - META-INF |` Manifest.mf `- Test` --test.class2. Creating an executable JAR file package to make 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. Actually, if the developer can make an executable JAR file package to the user, it is convenient to use it. 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, as as an example of the above Test directory, the command is as follows: Jar CVFM Test.jar Manifest.mf Test here Test.jar and Manifest.mf two files, respectively It is the corresponding parameter 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: Execute the main class full name (including the package name) For example, assume that the test.class in the above example belongs to the TEST package, and is an executable class (define 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 need There is Main-Class: Test.Test one, and the line is over with a back-end. After creating the manifest.mf file, our directory structure becomes: == | - TEST | `- Test.class` - manifest.mf This time, you need to use the JAR command to use JAR commands in the upper directory of the Test directory. Create a JAR file package. That is, in the directory of "==" represented in the directory tree, use the following command: JAR CVFM Test.jar Manifest.mf Test created Test.jar in the "==" directory, this test.jar is executing 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 to run this class before you don't packet JAR files. In the above example, Java Test.test is the correct operation (of course, in the correct case where ClassPath is correct). 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 Sun has helped us do well. We don't even need to put Tools.jar in the ClassPath.

Use without any jar commands We can see the usage of JAR commands as follows: jar {ctxu} [vfm0m] [jar- file] [manifest- file] [-c directory] file name ... where {ctxu} is Jar The subcommand of the command, each JAR command can only contain one of the CTXU, which represents: -c Creating a new JAR file package -T listing the content list of the JAR file package -X expands the specified file of the JAR file package or all File -U update the option existing JAR file (add file to the JAR file) [VFM0M] can optionally, you can not choose, the option parameter of the JAR command is the selection of detailed report and print to standard Output -f Specifies the JAR file name, usually this parameter is a must-mode specifying the Manifest manifest file -0 stored, not compressed, which is not compressed, the JAR file package is larger than the volume of the parameter, but the speed is more Fast -m does not generate a list of all items (MANIFEST) files, this parameter ignores the -m parameter [JAR-file] needs to be generated, view, update, or unlocked JAR file package, it is the accessory parameter of the -f parameter [ Manifest- File] The Manifest list file, which is the accessory parameter of the -m parameter [-c Directory] indicates that the operation of the specified directory is executed. It is equivalent to using the CD command to turn this directory without carrying without -C parameter jar command, it can only be available when creating and updating the JAR file package. File name ... Specify a file / directory list, these files / directory are to add files / directories in the JAR file. If the directory is specified, then the JAR command will automatically put all the files and subdirectories in the directory in the package when the JAR command is packaged. Some examples will be used to illustrate the usage of JAR commands: 1) jar cf test.jar Test This command is not executed The process of the process, the execution result is a 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 the V parameter, the package process is displayed, as follows: MANIFEST, the manifest) Add: TEST / (read = 0) (write = 0) (0%) Add: test / test.class (read = 7) (write = 6) (40%) 3) JAR CVFM TEST.JAR TEST This command is similar to 2) However, but there is no meta-inf / manifest file in the generated Test.jar. The information for the package process is also slightly different: increase: TEST / (read = 0) (write = 0) (store 0%) increase : Test / test.class (read = 7) (write = 6) (40%) 4) JAR CVFM TEST.JAR Manifest.mf Test Run Results and 2) Similar, display information is the same, just generate JAR The meta-inf / manifest content in the package is the content that contains manifest.mf. 5) JAR TF TEST.JAR In the case of Test.jar already exists, you can view the contents in Test.jar, such as 2) and 3) The resulting test.jar should be this command, and the results are as follows; for 2) meta-inf / meta-inf / manifest.mftest / test / test.class For 3) Test / Test / Test.class6) JAR TVF Test. In addition to the content displayed in JAR, it also includes details of the package, 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.MF0 WED JUN 19:33:04 GMT 2002 TEST / 7 WED JUN 19 15:33:04 GMT 2002 Test / Test.class7) JAR XF Test.jar Unope Test.jar to the current directory, not displayed Any information, for 2) generated test.jar, the unlocked directory structure is as follows: == | - META-INF | `- Manifest` - Test `--test.class8) Jar XVF Test.jar run The result is the same as 7), which is detailed for the decompression process. Fine information display, such as: Meta-INF / Expansion: meta-inf / manifest.mf creation: test / expand: test / test.class9) jar uf test.jar manifest.mf Add file Manifest in Test.jar .mf, 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.

转载请注明原文地址:https://www.9cbs.com/read-130942.html

New Post(0)