"Crazy Java Handout" (18)----JAR file

Source: Internet
Author: User

    • Using JAR files

Jar file Full name Java ARchive file, Java Archive. Typically a jar file is a compressed file, often called a jar package. The difference between a jar file and a zip file is that the jar file contains a default name of Meta-inf/manifest. MF manifest file, which is automatically created by the system when the jar file is generated.

When an application is developed, the application contains many classes, and if it is necessary to make the application available to others, it is usually packaged into a jar file, which is provided to others for use. As long as someone else adds the jar file to the system's CLASSPATH environment variable, the JVM can automatically unpack the jar in memory and use the jar as a path to find the path structure of the class or package hierarchy that is required.

The jar is automatically installed with the JDK in the bin directory under the JDK installation directory.

    • Detailed jar command
    1. Create jar file: Jar CF Test.jar Test

The command does not show the compression process. Generates a Test.jar file for the entire contents of the test path under the current path. If the Test.jar file already exists in the current directory, the file will be overwritten.

2. Create the jar file and display the compression process: Jar CVF Test.jar Test

The same 1 command, but because of the-v parameter, the packaging process is displayed.

3. Not in the manifest file: Jar CVFM Test.jar Test

The same 2, but the M option indicates that the manifest file is not generated, so the Test.jar package does not contain meta-inf/manifest. MF file, the packaging process information is also slightly different.

4. Customizing the contents of the manifest file: Jar CVFM test.jar MANIFEST.MF test

Similar to 2, the display information is the same, but the lowercase m option specifies that the user manifest file information is read, so the manifest file Meta-inf/manifest in the generated jar package. The content of MF is different, it will add the content of custom MANIFEST.MF file based on the list file that is called out.

The contents of the list are composed of key:< spaces >value, each line can only define one key-value pair, the key-value of each row cannot have spaces before, and must be shelf written. The file content cannot be blank at the beginning, and the file must end with a blank line.

5. View jar Package Contents: Jar TF Test.jar

6. View the jar package details (size, last update time): Jar TVF Test.jar

7. Unzip: Jar XF Test.jar

8. Extract with message: Jar XVF Test.jar

9. Update the jar file: Jar uf Test.jar Hello.class

If the Hello.class file is already in Test.jar, replace the original Hello.class file with the new Hello.class file if Test.jar The new Hello.class file is added to the Test.jar file without the Hello.class file.

10. Show details when updating: Jar UVF Test.jar Hello.class

    • Create an executable jar package

When an application is successfully developed, there are roughly three ways to publish it:

    1. Use the platform-related compilers to compile the entire application into platform-related executable files. This approach often requires third-party compiler support, and the executable file generated by the compiler loses cross-platform features and may even have some performance degradation.
    2. Compile a batch file (bash script) for your app, for example: Java package. MainClass when the user clicks the bash script above, the Java command executes, thus running the program main class
    3. An application is made into an executable jar package to publish the application through a jar package. This is also a more typical approach.

  

The key to creating an executable jar package is to have the JAVAW command know which class in the jar package is the main class, and the JAVAW command can run the program by running the main class. The jar command has an-e option that specifies the class name of the jar Yoshitada as the main class of the program Portal:

Jar Cvfe Test.jar Test *.class

The above command compresses all *.class files in the current directory into the Test.jar package and specifies that the test class be used as the entry for the program.

  

    • Jar Pack Tips

Under Linux, use unzip to extract the jar package, if you need to extract the files to the specified directory, you only need to specify a-D option.

"Crazy Java Handout" (18)----JAR file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.