Jar package generated by java
Can an application program written in Java eventually form an executable file similar to exe? Can it only be run through command lines? There are two types of Producer: 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 for compilation. 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. Next let's take a look at what is a JAR File package: 1. the JAR File of the JAR File package is a Java Archive File, which is closely related to Java and is 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 some files with the following directory structure: = -- test -- Test. class compresses the ZIP file into a zipfile test.zip. The internal directory structure of the ZIP file is test.zip '-- test' -- test. class if we use the JDK jar command to compress it into a JAR file 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 and create an executable JAR package to publish your program. This is the most typical usage of a 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 is installed in Windows (Java Runtime environment.pdf, the installation file will map .jarfile to javaw.exe. For an executable JAR package, you only need to double-click it to run the program. It is as convenient as reading the. chm document (. chm document is opened by hh.exe ). 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. Similarly, the test directory is used as an example. The command is as follows: jar cvfm test. jar manifest. mf test here test. jar and manifest. the two mf files are the corresponding parameters f and m, which 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 format of writing in MANIFEST is as follows: Main-Class: The full name of the executable Main Class (including the package name). For example, suppose 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 stored in any location or another file name. You only need to have Main-Class: test. A line of Test, and the line ends with a carriage return. Created manifest. after the mf file, our directory structure becomes: ==| -- test | '-- Test. class '-- manifest. 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, run the following command: jar cvfm test. jar manifest. after mf test, test is created in the "=" directory. jar, the test. jar is the executed JAR file package. 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 ).