Java использование jar файл

Java использование jar файл

You can use the jar command to create an archive for classes and resources, and to manipulate or restore individual classes or resources from an archive.

jar [OPTION. ] [ [--release VERSION] [-C dir] files] . 

The jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. Initially, the jar command was designed to package Java applets (not supported since JDK 11) or applications; however, beginning with JDK 9, users can use the jar command to create modular JARs. For transportation and deployment, it’s usually more convenient to package modules as modular JARs.

The syntax for the jar command resembles the syntax for the tar command. It has several main operation modes, defined by one of the mandatory operation arguments. Other arguments are either options that modify the behavior of the operation or are required to perform the operation.

When modules or the components of an application (files, images, and sounds) are combined into a single archive, they can be downloaded by a Java agent (such as a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. The jar command also compresses files, which further improves download time. The jar command also enables individual entries in a file to be signed so that their origin can be authenticated. A JAR file can be used as a class path entry, whether or not it’s compressed.

Читайте также:  Google chrome index php

An archive becomes a modular JAR when you include a module descriptor, module-info.class , in the root of the given directories or in the root of the JAR archive. The following operations described in Operation Modifiers Valid Only in Create and Update Modes are valid only when creating or updating a modular JAR or updating an existing non-modular JAR:

All mandatory or optional arguments for long options are also mandatory or optional for any corresponding short options.

When using the jar command, you must specify the operation for it to perform. You specify the operation mode for the jar command by including the appropriate operation arguments described in this section. You can mix an operation argument with other one-letter options. Generally the operation argument is the first argument specified on the command line.

-i= FILE or —generate-index= FILE

Generates index information for the specified JAR file.

Lists the table of contents for the archive.

Updates an existing JAR file.

Extracts the named (or all) files from the archive.

Prints the module descriptor or automatic module name.

Operation Modifiers Valid in Any Mode

You can use the following options to customize the actions of any operation mode included in the jar command.

Changes the specified directory and includes the files specified at the end of the command line.

jar [OPTION. ] [ [--release VERSION] [-C dir] files]

Specifies the archive file name.

Creates a multirelease JAR file. Places all files specified after the option into a versioned directory of the JAR file named META-INF/versions/ VERSION / , where VERSION must be must be a positive integer whose value is 9 or greater.

At run time, where more than one version of a class exists in the JAR, the JDK will use the first one it finds, searching initially in the directory tree whose VERSION number matches the JDK’s major version number. It will then look in directories with successively lower VERSION numbers, and finally look in the root of the JAR.

Sends or prints verbose output to standard output.

Operation Modifiers Valid Only in Create and Update Modes

You can use the following options to customize the actions of the create and the update main operation modes:

-e= CLASSNAME or —main-class= CLASSNAME

Specifies the application entry point for standalone applications bundled into a modular or executable modular JAR file.

Includes the manifest information from the given manifest file.

Doesn’t create a manifest file for the entries.

Specifies the module version, when creating or updating a modular JAR file, or updating a non-modular JAR file.

Computes and records the hashes of modules matched by the given pattern and that depend upon directly or indirectly on a modular JAR file being created or a non-modular JAR file being updated.

-p path or —module-path path

Specifies the location of module dependence for generating the hash.

Reads jar options and file names from a text file.

Operation Modifiers Valid Only in Create, Update, and Generate-index Modes

You can use the following options to customize the actions of the create ( -c or —create ) the update ( -u or —update ) and the generate-index ( -i or —generate-index= FILE ) main operation modes:

Stores without using ZIP compression.

The following options are recognized by the jar command and not used with operation modes:

Displays the command-line help for the jar command or optionally the compatibility help.

Displays help on extra options.

Prints the program version.

Examples of jar Command Syntax

Create an archive, classes.jar , that contains two class files, Foo.class and Bar.class .

jar --create --file classes.jar Foo.class Bar.class

Create an archive, classes.jar , by using an existing manifest, mymanifest , that contains all of the files in the directory foo/ .

jar --create --file classes.jar --manifest mymanifest -C foo/

Create a modular JAR archive, foo.jar , where the module descriptor is located in classes/module-info.class .

 jar --create --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/classes resources
 jar --update --file foo.jar --main-class com.foo.Main --module-version 1.0 -C foo/module-info.class

Create a versioned or multi-release JAR, foo.jar , that places the files in the classes directory at the root of the JAR, and the files in the classes-10 directory in the META-INF/versions/10 directory of the JAR.

In this example, the classes/com/foo directory contains two classes, com.foo.Hello (the entry point class) and com.foo.NameProvider , both compiled for JDK 8. The classes-10/com/foo directory contains a different version of the com.foo.NameProvider class, this one containing JDK 10 specific code and compiled for JDK 10.

Given this setup, create a multirelease JAR file foo.jar by running the following command from the directory containing the directories classes and classes-10 .

jar --create --file foo.jar --main-class com.foo.Hello -C classes . --release 10 -C classes-10 .

The JAR file foo.jar now contains:

% jar -tf foo.jar META-INF/ META-INF/MANIFEST.MF com/ com/foo/ com/foo/Hello.class com/foo/NameProvider.class META-INF/versions/10/com/ META-INF/versions/10/com/foo/ META-INF/versions/10/com/foo/NameProvider.class 

As well as other information, the file META-INF/MANIFEST.MF , will contain the following lines to indicate that this is a multirelease JAR file with an entry point of com.foo.Hello .

. Main-Class: com.foo.Hello Multi-Release: true

Assuming that the com.foo.Hello class calls a method on the com.foo.NameProvider class, running the program using JDK 10 will ensure that the com.foo.NameProvider class is the one in META-INF/versions/10/com/foo/ . Running the program using JDK 8 will ensure that the com.foo.NameProvider class is the one at the root of the JAR, in com/foo .

Create an archive, my.jar , by reading options and lists of class files from the file classes.list .

To shorten or simplify the jar command, you can specify arguments in a separate text file and pass it to the jar command with the at sign ( @ ) as a prefix.

jar --create --file my.jar @classes.list

Источник

Using JAR Files: The Basics

JAR files are packaged with the ZIP file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you can realize many JAR file benefits using only these basic features.

Even if you want to take advantage of advanced functionality provided by the JAR file format such as electronic signing, you’ll first need to become familiar with the fundamental operations.

To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit (JDK). Because the Java Archive tool is invoked by using the jar command, this tutorial refers to it as ‘the Jar tool’.

As a synopsis and preview of some of the topics to be covered in this section, the following table summarizes common JAR file operations:

AppletClassName.class archive width=width height=height> 

This section shows you how to perform the most common JAR-file operations, with examples for each of the basic features:

Creating a JAR File

This section shows you how to use the Jar tool to package files and directories into a JAR file.

Viewing the Contents of a JAR File

You can display a JAR file’s table of contents to see what it contains without actually unpacking the JAR file.

Extracting the Contents of a JAR File

You can use the Jar tool to unpack a JAR file. When extracting files, the Jar tool makes copies of the desired files and writes them to the current directory, reproducing the directory structure that the files have in the archive.

Updating a JAR File

This section shows you how to update the contents of an existing JAR file by modifying its manifest or by adding files.

Running JAR-Packaged Software

This section shows you how to invoke and run applets and applications that are packaged in JAR files.

Additional References

The documentation for the JDK includes reference pages for the Jar tool:

Источник

Lesson: Packaging Programs in JAR Files

The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.

The JAR file format provides many benefits:

  • Security: You can digitally sign the contents of a JAR file. Users who recognize your signature can then optionally grant your software security privileges it wouldn’t otherwise have.
  • Decreased download time: If your applet is bundled in a JAR file, the applet’s class files and associated resources can be downloaded to a browser in a single HTTP transaction without the need for opening a new connection for each file.
  • Compression: The JAR format allows you to compress your files for efficient storage.
  • Packaging for extensions: The extensions framework provides a means by which you can add functionality to the Java core platform, and the JAR file format defines the packaging for extensions. By using the JAR file format, you can turn your software into extensions as well.
  • Package Sealing: Packages stored in JAR files can be optionally sealed so that the package can enforce version consistency. Sealing a package within a JAR file means that all classes defined in that package must be found in the same JAR file.
  • Package Versioning: A JAR file can hold data about the files it contains, such as vendor and version information.
  • Portability: The mechanism for handling JAR files is a standard part of the Java platform’s core API.

This lesson has four sections:

Using JAR Files: The Basics

This section shows you how to perform basic JAR-file operations, and how to run software that is bundled in JAR files.

Working with Manifest Files: The Basics

This section explains manifest files and how to customize them so you can do such things as seal packages and set an application’s entry point.

Signing and Verifying JAR Files

This section shows you how to digitally sign JAR files and verify the signatures of signed JAR files.

This section introduces you to some of the JAR-handling features of the Java platform. The JAR file format is an important part of the Java platform’s extension mechanism. You can learn more about that aspect of JAR files in the The Extension Mechanism trail of this tutorial.

Questions and Exercises: JAR

Test what you’ve learned about JAR.

Additional References

The documentation for the Java Development Kit (JDK) includes information about the Jar tool:

Источник

Оцените статью