Summary of jar package packaging under Java command line

Jar package packaging implementation

Jar package packaging can be realized by using jar instructions. Enter jar in the command line to view the contents of jar instructions

From the two examples shown at the end, we can see that there are two packaging methods. The difference between the two is whether to use the self-defined manifest file. The first example does not use the manifest file for packaging, so the manifest file in the final generated jar package is the default file. This method is applicable to a relatively simple jar package structure, there is no other jar package dependency, and the generated jar package does not need to be executable. The jar package generated in this way cannot use Java - jar XXX The. Jar command cannot be executed because the program entry is not specified in the manifest file. The second example is a commonly used packaging method, that is, using a custom manifest file to participate in packaging, which can add dependencies to the package, specify a program entry, and implement Java - jar XXX Jar directly runs the jar package.

The first simple packaging method

The simplest is to package and output the compiled class bytecode file in the current folder. The example is as follows: write three java files, test1 java test2. Java and main java

And test2 Java file

Main. java

Compile these three files on the command line and use javac command to compile.

Package the compiled class file with jar instructions

The added list is displayed during packaging. Use the decompression tool to open the generated test Jar package, you can see the following structure:

In addition to the three compiled class files, there is a meta-inf folder with a manifest MF (manifest file). The role of this file is very important, which will be described later. Let's look at the contents first

A very simple list that contains only the list version and the Java version.

At this time, execute Java - jar test Jar has the following effects:

No error is reported for the main list attribute. This is because we use the first method to generate jars and use the default list. The default list does not specify a program entry, so an error occurs. You can directly change the manifest file in the jar package (open it with the decompression tool and save it after changes) to the following effect:

Execute Java - jar test again After jar, enter the correct content into the program:

In the manifest file, the main class attribute is added to specify the program entry, so as to directly execute the jar file. Therefore, you cannot directly execute jar files by using the default manifest. You can either package with your own defined manifest file or change the manifest file in the package.

The second packaging method

The second packaging method is more general. Generally, the first line of the java file is package XXX; That is, the package name also determines the path where the compiled class file exists. When there are multiple java files to be compiled and packaged and they have different package names, it is unrealistic to write one file by one when packaging according to the first method, so there is the second method. Put the existing directories of all class files to be packaged and the dependent jar packages in a root folder (such as foo), and then write the manifest file to specify the program entry and other dependent jar packages. When executing instructions:

Note that there is a space and a dot after the foo / folder in the above instruction

Let's look at an example

Also test1 Java and test2 Java and main Java, but each has its own package name.

Also compiled with javac instructions, the three class files exist in different paths because they have different package names. Put all the folders containing class files with compilation numbers under the foo folder:

Then write a manifest file outside foo:

The contents of manifest are as follows:

Note: the last line of the manifest file is empty.

Execute the command from the command line: jar cvfm test jar MANIFEST. MF -C foo/ .

Test whether the jar package can run directly on the command line. Use the command Java - jar test jar

Package the jar correctly and run the jar successfully

Introduction to manifest file

Through the above two examples, you can see that the manifest file is necessary for jar packaging. The manifest file describes the details of the packaged jar file, which exists in the folder of the packaged meta - inf The main contents of a simple manifest file are as follows:

The main attributes are manifest version main class path, which are very important when making jar packages Manifest version is the version number. Just write it. Main class is the entry program of the jar package, specifying the full name of the running class (must include the package name), so you can use Java - jar name Jar directly runs the jar package. The third class path refers to other jar packages that you need to rely on when packaging. Your own program may also contain other jar packages when packaging, so you need to add dependencies.

Note that there is a space between the colon and the content of each manifest attribute, and a blank line should be left at the end after writing, otherwise the main list attribute will not be found at runtime

Summary

The error prone part of jar file packaging is the compilation of manifest file, which is prone to some format errors, such as less space between colon and content of attribute, no space between adding dependencies in class path, too many dependent files, no space at the beginning of each line when writing multiple lines, no empty line at the last line of the file, etc. When writing the manifest file, pay attention to these key points so that it won't take too much time to package.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>