Powerful functions of JAR File Format

Source: Internet
Author: User

Most Java programmers are familiar with the basic operations on JAR files. However, only a few programmers know the powerful functions of the JAR file format. In this article, the author discusses many functions and advantages of the JAR format, including packaging, executable JAR files, security, and indexing.

What is a JAR file?

The JAR file format is based on popular ZIP file formats and is used to aggregate many files into one file. Unlike ZIP files, JAR files are not only used for compression and release, but also for deployment and encapsulation of libraries, components, and plug-in programs, and can be directly used by tools such as compilers and JVM. JAR contains special files, such as manifests and deployment descriptor, to indicate how the tool handles specific JAR.

A jar file can be used:

  1. Used to publish and use Class Libraries
  2. As a building unit for applications and extensions
  3. As the deployment unit of components, applets, or plug-in programs
  4. Used to package auxiliary resources associated with components

The JAR file format provides many advantages and functions, many of which are not provided by traditional compression formats such as ZIP or TAR. They include:

Security. You can add a digital signature to the JAR file. In this way, the tool that can identify the signature can selectively grant you software security privileges, which cannot be done by other files. It can also detect whether the code has been tampered.

Reduce download time. If an applet is bound to a JAR file, the browser can download the applet Class file and related resources in an HTTP transaction, rather than opening a new connection to each file.

Compression. The JAR format allows you to compress files to improve storage efficiency.

Transmission Platform Extension. The Java extensionframework provides a method to add functions to the Java core platform, these extensions are packaged using JAR files (Java 3D and JavaMail are examples of extensions developed by Sun ).

Pack Seal. You can choose to seal the packages stored in the JAR file to enhance the consistency and security of the version. Sealing a package means that all classes in the package must be found in the same JAR file.

Package Version Control. A jar file can contain data about the files it contains, such as vendor and version information.

Portability. The mechanism for processing JAR files is the standard part of the core APIs of the Java platform.

Compressed and uncompressed JAR

The jar tool (for details, see the jar tool) compresses files by default. Uncompressed JAR files can generally be loaded faster than compressed JAR files, because files need to be decompressed during the loading process, but uncompressed files may be downloaded over the network for a longer time.

META-INF directory

Most JAR files contain a META-INF directory that is used to store package and extended configuration data such as security and version information. The Java 2 Platform recognizes and interprets the following files and directories in the META-INF directory to configure applications, extensions, and class loaders:

MANIFEST. MF. This manifest file defines data related to extensions and packages.

INDEX. LIST. This file is generated by the new option-I of the jar tool. It contains the location information of the package defined in the application or extension. It is part of the implementation of JarIndex and is used by the class loader to accelerate the class loading process.

Xxx. SF. This is the signature file of the JAR file. The placeholder xxx identifies the signatory.

Xxx. DSA. The signature block file associated with the signature file, which stores the public signature used to sign the JAR file.

Jar Tool

To execute basic tasks in a JAR file, use the Java Archive Tool (jar Tool) provided as part of Java Development Kit ). Use the jar command to call the jar tool. Table 1 shows some common applications:

Table 1. Common jar tool usage and function commands

Use a separate file to create a JAR file jar cf jar-file input-file...

Create a JAR file jar cf jar-file dir-name in a directory

Create an uncompressed JAR file jar cf0 jar-file dir-name

Update a JAR file jar uf jar-file input-file...

View the content of a JAR file. jar tf jar-file

Extract the content of a JAR file. jar xf jar-file

Extract the specified JAR xf jar-file archived-file from a jar file...

Run java-JAR app. jar

Executable JAR

An executable jar file is a self-contained Java application, which is stored in a specially configured JAR file, it can be directly executed by JVM without extracting files or setting the class path in advance. To run an application stored in a non-executable JAR, you must add it to your class path and call the main class of the application by name. However, you can run an application without extracting the executable JAR file or knowing the main entry point. Executable JAR can facilitate the release and execution of Java applications.

Create executable JAR

It is easy to create an executable JAR. First, put all application code in a directory. Assume that the main class in the application is com. mycompany. myapp. Sample. Create a JAR file containing the application code and identify the main class. Therefore, create a file named manifest at a location (not in the application directory) and add the following line to it:

Main-Class: com. mycompany. myapp. Sample

Then, create a JAR file like this:

Jar cmf manifest ExecutableJar. jar application-dir

All you need to do is ExecutableJar. jar.

An executable JAR must be referenced by the menifest file header. If the-jar option is used, the environment variable CLASSPATH and all the class paths specified in the command line are ignored by JVM.

Start executable JAR

Now that we have packaged our application into an executable jar named ExecutableJar. JAR, we can use the following command to directly start the application from the file:

Java-jar ExecutableJar. jar

Pack Seal

Sealing a package in the JAR file means that all classes defined in the package must be found in the same JAR file. This allows the package author to enhance version consistency between packaging classes. Sealing also provides a way to prevent code tampering.

To seal the package, add a Name header to the package in the JAR manifest file, and add the Sealed header with the value of "true. Like an executable JAR, you can seal a JAR by specifying a manifest file with an appropriate Header element when creating the JAR, as shown below:

Name: com/samplePackage/

Sealed: true

The Name header identifies the relative path Name of the output packet. It ends with a "/" and is different from the file name. All headers before the first blank line after the Name header apply to the file or package specified in the Name header. In the above example, because the Sealed header appears after the Name header and there is no blank line in the middle, the Sealed header will be interpreted as only applied to the package com/samplePackage.

JVM throws a SecurityException if you try to load a class in the sealing package from a place other than the JAR file where the sealing package is located.

Extended Packaging

Extended functions have been added to the Java platform, and the extended mechanism has been added to the JAR file format. The extension mechanism allows the JAR file to specify other JAR files by using the Class-Path header in the manifest file.

Assume that extension1.jar and extension2.jar are two JAR files in the same directory. The manifest file of extension1.jar contains the following headers:

Class-Path: extension2.jar

This header indicates that the class in extension2.jar is the extension class of the class in extension1.jar. Classes in extension1.jar can call classes in extension2.jar, and do not require extension2.jar to be in the class path.

When loading the JAR using the extension mechanism, JVM will efficiently and automatically add the JAR referenced in the Class-Path header to the Class Path. However, the extension JAR path is interpreted as a relative path, so in general, the extension JAR must be stored in the same directory where the JAR that references it is located.

For example, if the class ExtensionClient references the class ExtensionDemo, It is bundled in a jar file named ExtensionClient. JAR, and the class ExtensionDemo is bundled in ExtensionDemo. jar. To enable ExtensionDemo. jar to be extended, the ExtensionDemo. jar column must be

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.