This article illustrates the Java method for generating jar packs, which is a very practical technique. Share to everyone for your reference. The specific analysis is as follows:
Many Java beginners will have the question: Java written by the application program can eventually form an EXE-like executable file, can only use the command line to run?
There are usually two ways to make an executable jar package and then double-click it to run like a. chm document, and the other is to use Jet for compilation. But Jet is bought with money, and it is said that jet is not able to compile all the Java programs into execution files, and performance discounts. So, the way to make an executable jar package is the best choice, not to mention the ability to maintain the cross-platform nature of Java.
Let's take a look at what a jar file package is:
1. jar File Package
The jar file is Java Archive file, as implies meaning, its application is related to Java, is a Java document format. The jar file is very similar to a zip file--it's exactly the zip file, so call it a package. The only difference between a jar file and a zip file is that it contains a meta-inf/manifest in the contents of the jar file. MF file, this file is created automatically when the jar file is generated. For example, if we have some files with the following directory structure:
==
--Test
--Test.class
Compress it into a zip file test.zip, the internal directory structure of this zip file is:
Test.zip
'--Test
'--Test.class
If we use the JDK jar command to make it a jar package Test.jar, the internal directory structure for this jar file is:
Test.jar
|--Meta-inf
| '--MANIFEST. Mf
'--Test
'--test.class
2. Create an executable jar file package
Making an executable jar package to publish your program is the most typical use of a jar package.
A Java program is made up of several. class files. These. class files must be hierarchically stored according to the packages they belong to; Before running, you need to assign the root directory of all the used packages to the CLASSPATH environment variable or the-CP parameter of the Java command; run with the console. Run with Java commands, if you need to double-click to run the Windows must be written Batch file (. bat) or a Linux shell program. As a result, many people say that Java is a user-friendly programming language for developers.
In fact, if the developer can make an executable jar package to the user, then the user is easy to use. When you install the JRE (Java Runtime Environment) under Windows, the installation file maps the. jar file to the Javaw.exe open. Then, for an executable jar package, the user can simply double-click it to run the program, as easily as reading a. chm document (. CHM documents are opened by default by hh.exe). So, the key now is how to create this executable jar package.
To create an executable jar package, you need to use the jar command with the CVFM parameter, as in the above test directory, for example, the following command:
Jar CVFM Test.jar MANIFEST.MF test
Here Test.jar and manifest.mf two files, respectively corresponding to the parameters F and M, whose plays are in MANIFEST.MF. Because it is not enough to create an executable jar package, it is not sufficient to specify a MANIFEST.MF file because manifest is a feature of the jar package, and both executable jar packages and the not-executed jar packages contain manifest. The key is to execute the manifest of the jar package, which contains the contents of Main-class. This is written in the manifest format as follows:
Main-class: Executable main class full name (including package name)
For example, if the test.class in the previous example belongs to the test package and is an executable class (the public static void Main (string[) method is defined), then the MANIFEST.MF can be edited as follows:
Main-class:test. Test < return >
This manifest.mf can be placed anywhere, or it can be a different file name, and only needs to be main-class:test. Test row, and the line ends with a carriage return. After creating the MANIFEST.MF file, our directory structure becomes:
==
|--Test
| '--Test.class
'--MANIFEST.MF
At this point, you need to go to the parent directory of the test directory to use the JAR command to create the jar package. That is, the directory in the directory tree that uses "= =", use the following command:
Jar CVFM Test.jar MANIFEST.MF test
Test.jar is then created in the "= =" directory, and this test.jar is the executing jar package. You only need to use the Java-jar Test.jar command at run time.
Note that the created jar package needs to contain a complete directory structure that corresponds to the package structure of the Java program, as in the previous example. The class specified by Main-class must also be a complete class name that contains the package path, as in the previous example test. Test, and you can use Java < class name > to run this class before it is made into a jar package, that is, Java test in the example above. Test can run correctly (of course, in the case of CLASSPATH correct).
I hope this article will help you with your Java programming.