Java method for generating jar packages

  • 2020-04-01 03:30:31
  • OfStack

This article illustrates how Java generates jar packages, which is a very useful technique. Share with you for your reference. Specific analysis is as follows:

Many Java beginners will wonder whether an application written in Java will end up as an exe-like executable that can only be run from the command line.

There are usually two ways to do this. One is to make an executable JAR file package and then double-click it like a.chm document. The other is to compile using JET. But JET costs money, and it is said that JET does not compile all Java programs into executable files, and its performance is somewhat compromised. Therefore, using the method of producing executable JAR file packages is the best choice, and it preserves the cross-platform nature of Java.

Here's a look at what a JAR package is:

1. JAR file package

The JAR File is the Java Archive File, which is closely related to Java and is a document format of Java. A JAR file is very much like a ZIP file -- it is exactly a ZIP file, so it's called a package. The only difference between a JAR file and a ZIP file is that in the contents of the JAR file, there is a meta-inf/manifest.mf file that is automatically created when the JAR file is generated. For example, if we have some files in the following directory structure:
= =
- the test
-- the Test. The class
Compress it into the ZIP file test.zip, then the internal directory structure of the ZIP file is:
The test.zip
` - test
` -- Test. Class

If we use the JDK's jar command to call it test.jar, the internal directory structure of the jar file is:
Test. The jar
| - meta-inf
| ` -- the MANIFEST. MF
` - test
` -- Test. Class

2. Create an executable JAR file package

Making an executable JAR file package to distribute your program is the most typical use of a JAR file package.

A Java program is composed of several.class files. These. Class files must be categorized according to the package to which they belong. Before running, you need to assign the root directory of all the packages you use to the CLASSPATH environment variable or the -cp parameter of the Java command. The runtime also goes down to the console to run using Java commands, and if you need to directly double-click to run, you must write a Windows batch file (.bat) or a Linux Shell program. As a result, many say Java is a programming language that is convenient for developers at the expense of users.

In fact, if the developer can make an executable JAR file package and give it to the user, it will be easier for the user to use. When you install the JRE(Java Runtime Environment) under Windows, the installation file maps the. Jar file to javaw.exe. Then, for an executable JAR file package, the user can simply double-click on it to run the program, just as easily as reading the.chm document (.chm document is opened by hh.exe by default). The key now, then, is how to create this executable JAR file package.

To create an executable JAR file package, you need to use the JAR command with CVFM parameter. Again, take the above test directory as an example, the command is as follows:


jar cvfm test.jar manifest.mf test 

In this case, test.jar and manifest.mf are the corresponding parameters f and m respectively, and they focus on manifest.mf. Because to create an executable JAR file package, it is not enough to specify a manifest.mf file, because the manifest is a feature of the JAR file package, both the executable JAR file package and the non-executable JAR file package contain the manifest. The key is the MANIFEST of the executable JAR file package, whose contents contain a main-class entry. This is written in the MANIFEST as follows:


Main-Class:  Executable main class full name ( Include the package name ) 

For example, if the test.class in the above example belongs to the Test package and is an executable class (which defines the public static void main(String[]) method), then the manifest.mf can be edited as follows:


Main-Class: test.Test < enter > 

The manifest.mf can be anywhere, or any other file name, with just a line called main-class: test.test and ending with a carriage return. After creating the manifest.mf file, our directory structure changes to:
= =
| - test
| ` -- Test. Class
` -- the manifest. Mf

At this point, you need to go to the test directory's parent directory and use the jar command to create the jar file package. In the directory tree represented by "==", use the following command:


jar cvfm test.jar manifest.mf test 

The test.jar is then created in the "==" directory, and the test.jar is the executed jar file package. The runtime only needs to use the java-jar test.jar command.

It is important to note that the JAR file package you create needs to contain the complete directory structure corresponding to the package structure of the Java program, as in the example above. The Class specified by main-class must also be the full Class name with the package path, such as test.test in the example above. And you can use Java < The name of the class > To run the class, which means that in the above example Java test.test will run correctly (if the CLASSPATH is correct, of course).

I hope this article has been helpful to your Java programming.


Related articles: