command line package jar

    It is more convenient to use Eclipse to automatically package jars, but if there are no restrictions, the jars may contain many other redundant files. Therefore, you can customize the packaging style and use the jar command on the command line to complete the java program packaging.


1. Simple packaging

    Create a new directory tmp and enter the directory.

    Create a new Hello.java file with the following contents:

public class Hello{
	public static void main(String[] args){
		System.out.println("Hello World");
	}
}

   Use the command line in the current directory:

    

    The files in the directory should now look like this:

    

    Open a1.jar on the command line:

    

    This is because the manifest file was not included in the packaging before, so the program does not know the main class

    The following is to add a list to the previous program. First, create a new file manf in the current directory with the content Main-Class: Hello

    

    The specification of the manifest file, there should be a space after the colon, no space at the end of the line, and the last line is a blank line

    Then the current directory looks like this:

    

    Repackage:

    

This will output the result.


Second, refer to the class file

    The above is just a simple class for packaging. What if one class references another class?

    Create a new directory test in the current directory, and then create a new Get.java file in the test directory with the content:

public class Get{
	public void print(){
		System.out.println("reference class");
	}
}

    At the same time, modify the previous Hello.java so that the file refers to the Get class. The modified Hello.java should look like this:

import test.Get;
public class Hello{
	public static void main(String[] args){
		Get aget=new Get();
		act.print();
	}
}

Let's start compiling the above two class files:


It can be found that Get.java is compiled correctly, but an error is reported when compiling Hello.java. This is because test is treated as a package in import test.Get in Hello.java, but there is no declaration to surround test in Get.java, so find Not the Get.class file under the test package. The correct way is to re-modify the Get.java file, add package test to the file, and recompile.


 

Third, refer to the jar package

    For a packaged Jar that is not a class file, how to include the referenced jar to complete the packaging?

    First of all, there is a test/Get.class file in the a3.jar packaged above, so we can delete the test directory in the current directory, roughly as follows:

    

      Since the test directory was removed, Import test.Get in the Hello.java file is actually for a3.jar because the test directory only has .

    So we need to include a3.jar in the package. We can get an error when we are about to compile Hello.java

    

    This is because we just deleted the test directory. Of course, the package test cannot be found when compiling Hello.java. But we know that the test directory exists in a3.jar, so we can include a3.jar to compile and run:

    

    After it can be packaged normally, an error is reported when using it. The solution is to specify the class loading path in the manifest file and include the path of the Jar.

    

    Repack:

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645649&siteId=291194637