jar package implementation
The jar package can be packaged using the jar directive. Entering jar in the command line can view the content of the jar directive
From the two examples shown in the last show, there are two ways to package. The difference between the two is whether to use the MANIFEST manifest file you defined. The first example is not packaged using the MANIFEST file, so the MANIFEST file in the final generated jar package is the default file. This method is suitable for 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 be executed using the java -jar XXX.jar command because the program entry is not specified in the MANIFEST file. The second instance is a relatively common packaging method, that is, use a custom MANIFEST file to participate in the packaging, so that you can add dependencies to the package, and specify the program entry to implement java -jar XXX.jar to directly run the jar package.
The first simple packaging method
The easiest thing is to package and output the compiled class bytecode file in the current folder. Examples are as follows:
Write three java files, test1.java test2.java and Main.java
public class test1{ public static void main(String[] args) { } public void display() { System.out.println("this is class test1"); }}and test2.java file
public class test2{ public static void main(String[] args) { } public void display() { System.out.println("this is class test2"); }}Main.java
public class Main{ public static void main(String[] args) { for(String a:args) { System.out.println("Gived parameter"+a); } test1 t1 = new test1(); t1.display(); test2 t2 = new test2(); t2.display(); }} Compile these three files on the command line and use the javac command to compile.
Use the jar directive to package the compiled class file
The added list is displayed during packaging. Use the decompression tool to open the generated test.jar package and you can see the following structure:
In addition to the compiled three class files, there is an additional META-INF folder, which contains a MANIFEST.MF (list file). The role of this file is very important, as explained later. Let's look at the contents in it first
Very simple manifest, only contains manifest version and java version.
Execution of java -jar test.jar at this time has the following effect:
No main list attribute error reported. This is because we use the first method to generate the jar using the default manifest. The default manifest does not specify the program entry, so an error occurs.
You can directly change the MANIFEST file in the jar package (open the decompression tool and save it after changing it), and change it to the following effect:
After executing java -jar test.jar again, the program enters the correct content:
The Main-Class attribute is added to the MANIFEST file and specifies the program entry, which realizes the direct execution of the jar file.
Therefore, using the default MANIFEST cannot directly execute the jar file. Either use the MANIFEST file you define to package it or change the MANIFEST file in the package.
The second way to pack
The second packaging method is more general. Generally speaking, the first line of the java file is package XXX; that is, the package name, it also determines the path of the compiled class file. When there are multiple java files to be compiled and packaged and they have different package names, it is very unrealistic to write one file at a time when packaging it according to the first method, so there is a second method. Put all the directories that exist in the class files to be packaged and the dependency jar packages in a root folder (such as foo), and then write a MANIFEST manifest file to specify the program entrance and other dependency jar packages added. In executing the command:
Note that there is a space and a dot behind foo/ folder in the above command
Let's see an example below
The same is test1.java, test2.java and Main.java, but each has its own package names.
package cn.mytest1;public class test1{ public static void main(String[] args) { } public void display() { System.out.println("this is class test1"); }} package cn.mytest2;public class test2{ public static void main(String[] args) { } public void display() { System.out.println("this is class test2"); }} package cn.mymain;import cn.mytest1.test1;import cn.mytest2.test2;public class Main{ public static void main(String[] args) { for(String item:args) { System.out.println("Passing parameter"+item); } test1 t1 = new test1(); test2 t2 = new test2(); t1.display(); t2.display(); }} Also compiled using javac directive, the three class files exist in different paths because their package names are different. Put all the compiled folders containing class files in the foo folder:
Then write a MANIFEST file outside foo:
The content of MANIFEST is as follows:
Note : The last line of the MANIFEST file is an empty line.
Execute the command from the command line: jar cvfm test.jar MANIFEST.MF -C foo/ .
Test whether the jar package can be run directly on the command line, use the command java -jar test.jar
Package correctly and run the jar successfully.
MANIFEST file introduction
Through the above two examples, we can see that the MANIFEST file is necessary for jar packaging. The MANIFEST file describes the detailed information of the packaged jar file and exists in the packaged META-INF folder. The main content of a simple MANIFEST file is as follows:
The main thing is that the three attributes of Manifest-Version Main-Class Class-Path are very important when making jar packages. Manifest-Version is the version number, just write it accordingly. Main-Class is the entry program of the jar package, which specifies the full name of the running class (must include the package name). In this way, you can use java -jar name.jar to run the jar package directly. The third Class-Path refers to other jar packages that need to be relied on when packaging. When packaging, your program may also contain other jar packages, so you need to add dependencies.
Note that there is a space between the colon and content of each MANIFEST attribute, and there must be a blank line at the end after writing, otherwise there will still be an error in the runtime that the main list attribute cannot be found.
summary
The easy thing to make mistakes in packaging jar files is the writing of Manifest manifest files, which are prone to format errors such as few spaces between the colon of the attribute and the content, no spaces between the addition of dependencies in Class-Path, too many dependencies, no spaces at the beginning of each line when writing multiple lines, no blank lines at the end of the file, etc. Pay attention to these key points when writing MANIFEST files, and you won’t spend too much time on packaging.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.