Jar basic operations

Source: Internet
Author: User

Pre-reading statement:

Http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

Basic operations on jar files

1. Create related

/> Jar CF jar-file input-file (s)

/*

Note: In windows and UNIX, the delimiter of the directory structure is different. The former is/and the latter is/. This document only describes the problem and does not strictly describe it. -- Note that

Platform, used in manifest files/

*/

/*

This command creates a jar-file in the current directory.

C switch specifies "CREATE" a JAR File

The F switch specifies the output to the file instead of stdout (standard output)

Jar-file if no suffix is specified, the default value is *. jar.

Space is used to separate multiple input files. The wildcard "*" can be used.

Specify the directory if necessary

The sequence in which the switch appears is not important, but if the M switch is enabled, it is another matter.

This command creates a manifest file by default.

*/

/*

For Java version 1, Jar files only support ASCII for file names, while Java version 2 supports UTF-8-encoded file names.

*/

/*

Other available switches for creation:

V when the JAR file is created, the redundant (verbose) output is generated in stderr (Java version 1) or stdout (Java version 2. Report the directory of Files Added to jar.

0 (zero) indicates not to compress.

M indicates that no default manifest file is generated.

M:

/> Jar CMF existing-manifest jar-file input-file (s)

-C changes the directory during the operation (only for Java version 2 ).

*/

/*

An example.

Suppose the following directory structure (in Windows ):

Tictactoe/tictactoe. Class (file)

Tictactoe/audio (sub-directory, under which all files are au files)

Tictactoe/images (subdirectory, all under this layer are GIF files)

If you want to press this plug-in into the jar file tictactoe. jar, it should be in the tictactoe directory (!) Run the following command:

Tictactoe> jar CVF tictactoe. Jar tictactoe. Class audio Images

The jar program automatically pushes the audio and images directories and Their subdirectories into the tictactoe. jar file, which appears in the tictactoe/tictactoe. jar file.

Because the verbose output switch is used, you can see the output similar to the following:

Adding: tictactoe. Class (in = 3825) (out = 2222) (deflated 41%)

Adding: Audio/(in = 0) (out = 0) (stored 0%)

Adding: Audio/beep. Au (in = 4032) (out = 3572) (deflated 11%)

Adding: Audio/ding. Au (in = 2566) (out = 2055) (deflated 19%)

Adding: Audio/return. Au (in = 6558) (out = 4401) (deflated 32%)

Adding: Audio/yahoo1.au (in = 7834) (out = 6985) (deflated 10%)

Adding: Audio/yahoo2.au (in = 7463) (out = 4607) (deflated 38%)

Adding: images/(in = 0) (out = 0) (stored 0%)

Adding: images/cross.gif (in = 157) (out = 160) (deflated-1%)

Adding: images/not.gif (in = 158) (out = 161) (deflated-1%)

If:

Tictactoe> jar cvf0 tictactoe. Jar tictactoe. Class audio Images

Indicates that only backup is not compressed.

 

If the directory contains all the files that need to be jar and their corresponding directory structure:

Tictactoe> jar CVF tictactoe. jar *

Is a simple way.

If you do not use the M or M switch, the default manifest file will be generated

META-INF/manifest. MF

The-C switch is used to jump to a directory to change the directory structure of the saved JAR file. Most of the files in different directory structures are aggregated as jar files. For example:

Tictactoe> jar CF imageaudio. jar-C images.-C audio.

The content and structure of the generated JAR file:

META-INF/manifest. MF

Cross.gif

Not.gif

Beep. au

Ding. au

Return. au

Yahoo1.au

Yahoo2.au

(Now let's assume there are subdirectories in the images subdirectory, as shown in the following figure:

Tictactoe/images/jpg

There are some *. jpg files in the JPG subdirectory. The above command results should be:

META-INF/manifest. MF

JPG/amakusa.jpg

JPG/ukyo.jpg

Cross.gif

Not.gif

Beep. au

Ding. au

Return. au

Yahoo1.au

Yahoo2.au

Illustrates the true meaning of-C.

)

*/

2. View jar content

/> Jar TF jar-File

/*

The T switch specifies the content table of the JAR file (table of contents ).

The F switch specifies that the JAR file is pointed out by the command line. If this switch is not used, the JAR file is expected to be a file name in stdin.

T has nothing to do with the order in which F appears, but there cannot be spaces between them.

This command outputs the content table of the corresponding JAR file to stdout.

The V switch can be used to view details about the file size and the latest change date.

*/

/*

Example.

Tictactoe> jar TF tictactoe. Jar

We will see in the standard output:

META-INF/manifest. MF

Tictactoe. Class

Audio/

Audio/beep. au

Audio/ding. au

Audio/return. au

Audio/yahoo1.au

Audio/yahoo2.au

Images/

Images/cross.gif

Images/not.gif

Note that all file structures in any operating system (Windows, Linux, or UNIX) are separated by forward slash. The path display in the jar file is relative

(Relative.

Another example.

Tictactoe> jar tvf tictactoe. Jar

Shown as follows:

256 Mon Apr 20 10:50:28 PDT 1998 META-INF/manifest. MF

3885 Mon Apr 20 10:49:50 PDT 1998 tictactoe. Class

0 wed Apr 15 16:39:32 PDT 1998 audio/

4032 wed Apr 15 16:39:32 PDT 1998 audio/beep. au

2566 wed Apr 15 16:39:32 PDT 1998 audio/ding. au

6558 wed Apr 15 16:39:32 PDT 1998 audio/return. au

7834 wed Apr 15 16:39:32 PDT 1998 audio/yahoo1.au

7463 wed Apr 15 16:39:32 PDT 1998 audio/yahoo2.au

0 wed Apr 15 16:39:44 PDT 1998 images/

157 wed Apr 15 16:39:44 PDT 1998 images/cross.gif

158 wed Apr 15 16:39:44 PDT 1998 images/not.gif

*/

3. Release files

/> Jar XF jar-file [archived-file (s)]

/*

The X switch is a sign of "releasing Files ".

F indicates that the file name of the JAR file to be released is provided by the command line, rather than stdin.

Jar-file can be a file with a path

[Archived-file (s)] indicates the content to be released. If this parameter is omitted, the content in jar-file will be completely released.

After being released, the original JAR file remains unchanged.

Note: When the file is released, the jar will overwrite the file with the same name in the target directory (but the directories and files with different names will not be washed away (-- overwrite principle !)).

*/

/*

Example.

Tictactoe> jar XF tictactoe. Jar tictactoe. Class images/cross.gif

This command does two things:

Generate a new copy of tictactoe in the current directory.

In the current directory, copy a new cross.gif file. If the directory already exists, only the last action is taken.

You can run the following command:

Tictactoe> jar XF tictactoe. Jar

Release the entire tictactoe. jar file.

*/

4. jar archive and corresponding description file (manifest file)

Jar archiving provides multiple functions, such as electronic signing, version control, package sealing, and extensions. The manifest file cannot be replaced.

.

Manifest is like a list of archived content. (The manifest is a special file that can contain information about the files packaged in a jar file.

)

The default and custom methods of the manifest file are described earlier.

Default manifest. MF (made in Java version 2 ):

------------

Manifest-version: 1.0

------------

It can be seen that the table items in the manifest file are in the form of "header: value", and the default manifest file table items (entries) include only one item describing the manifest itself (version ).

Both Java version 1 and Java version 2 correspond to the same manifest standard (manifest version 1 ). However, the default manifest files generated by jar of the two Java versions are different. For Java

In this example, manifest. mf is generated as follows:

------------

Manifest-version: 1.0

Name: Java/Math/bigdecimal. Class

SHA1-Digest: td1gzt8g11dxy2p4olszpc510864 =

MD5-Digest: z6z8xpj2aw/q9akrspf0cg =

Name: Java/Math/biginteger. Class

SHA1-Digest: obmrvikbnsxdnzzph5ilyf0s + be =

MD5-Digest: wfymhdkjnrenz4azdwwg1q =

------------

It can be seen that manifest. MF of Java version 1 describes every file in the jar package. Table items include the pathname and digest value of the file, which are generated by the file content.

). -- Note that there is no blank line between name and digest.

The Digest value is generally used to sign the JAR file (sign), but because it is not always necessary, it is removed in Java version 2. (To better structure the functions .)

In a word, the file information recorded by manifest depends entirely on the use of the jar package. (Exactly what file information is recorded in the manifest will

Depend on what use you intend for the JAR file.) You can modify the manifest file based on the role of the JAR file.

If you only use jar as a simple compression method (like a zip), you do not have to worry about manifest files.

Manifest is used for specific purposes (only for Java version 2 ):

1) Java applications are bundled as jar files (bundle)

Table item main-class: classname indicates the entry point of the program-class containing the public static void main (string [] ARGs) {} method.

2) download extensions)

The download extension is the JAR file (http://java.sun.com/docs/books/tutorial/ext/index.html) referenced by the manifest file of another jar file ).

For example, an applet is bound to a jar file. The manifest reference (pointing to) one or more other jar files (these files serve as an extension (or

Extensions) for the purpose of that applet ). Of course, extensions can be referenced with each other.

The downloading extension is specified by the table entry with the class-path header (header field) in the corresponding manifest file of the quote. For example:

Class-path: servlet. Jar Infobus. Jar Acme/beans. Jar

Note that the URLs in the class-path header are given relative to the URL of the JAR file of the applet or application.

3) package Sealing)

This means that all classes defined in the same package are packaged in the same JAR file. In this way, version consistency can be guaranteed or used as a security measure.

(Security measure) -- avoid the visibility of the default package (the current directory. After classpath is added ).

Seal a package in the following format:

----------------

Name: mycompany/mypackage/

Sealed: True

----------------

The value of the name header indicates the relative path name of the package ("package mycompany. mypackage ;"). (Note that it ends with a slash (/) to distinguish it from the file name ).

4) package Versioning)

See http://java.sun.com/products/jdk/1.2/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning

An example.

----------------

Name: Java/util/

Specification-title: "Java Utility Classes"

Specification-version: "1.2"

Specification-vendor: "Sun Microsystems, Inc .".

Implementation-title: "Java. util"

Implementation-version: "build57"

Implementation-vendor: "Sun Microsystems, Inc ."

----------------

Specification in manifest format:

Http://java.sun.com/products/jdk/1.2/docs/guide/jar/manifest.html

The following describes the M switch of the jar tool.

If you want to use the M switch, you must first prepare a text file used as the manifest. The m switch means Merge (merge), which is used to set the content of the file (!) Attached to the default (or existing

) Above the manifest (note not to replace !).

/> Jar CMF manifest-addition jar-file input-file (s)

/> Jar CFM jar-File Manifest-addition input-file (s)

Generate the same result. Pay attention to the ing between the switch and parameters!

An example is used to describe the seal of jar, manifest, and package ......

Suppose the JAR file is prepared to contain the following package (note that the package does not have a jar file and the Directory corresponds to it ):

Mycompany/firstpackage/

Mycompany/secondpackage/

Mycompany/thirdpackage/

Mycompany/fourthpackage/

If you want to seal firstpackage and thirdpackage, you should write a manifest like this:

Sealinfo

----------------

Name: mycompany/firstpackage/

Sealed: True

Name: mycompany/thirdpackage/

Sealed: True

----------------

Note that the text file must end with a line break with a carriage return, otherwise it is difficult to use the parse program.

Set the current directory to the parent directory of mycompany. Then,

> Jar CMF sealinfo myjar. Jar mycompany

The following manifest document is generated in myjar. jar:

----------------

Manifest-version: 1.0

Name: mycompany/firstpackage/

Sealed: True

Name: mycompany/thirdpackage/

Sealed: True

----------------

The first line comes from the default manifest, and the others are manually added.

5. Update the JAR File

The u Switch of Java version 2 is used to update an existing JAR file (by modifying its manifest or adding a new file ).

/> Jar UF jar-file input-file (s)

U indicates to update an existing JAR file.

Input-file (s) is the list of files to be added to jar-file, separated by spaces.

With this command, files with the same name and Path in jar-file will be overwritten.

As described in the preceding JAR file, you can use-C to switch the conversion path.

/> Jar Umf manifest1 jar-File

This command merges manifest1 into the manifest file of jar-file.

An example.

Tictactoe. Jar has the following content:

META-INF/manifest. MF

Tictactoe. Class

Audio/

Audio/beep. au

Audio/ding. au

Audio/return. au

Audio/yahoo1.au

Audio/yahoo2.au

Images/

Images/cross.gif

Images/not.gif

To add images/new.gif to the JAR archive, run the following command in the parent directory of images:

Tictactoe> jar UF tictactoe. Jar images/new.gif

The modified JAR archive should have the following content:

META-INF/manifest. MF

Tictactoe. Class

Audio/

Audio/beep. au

Audio/ding. au

Audio/return. au

Audio/yahoo1.au

Audio/yahoo2.au

Images/

Images/cross.gif

Images/not.gif

Images/new.gif

If,

Tictactoe> jar UF tictactoe. jar-C images new.gif

The command will first go to the images directory before the mongonew.gif file, and the archived results will not include the images path. Result:

META-INF/manifest. MF

Tictactoe. Class

Audio/

Audio/beep. au

Audio/ding. au

Audio/return. au

Audio/yahoo1.au

Audio/yahoo2.au

Images/

Images/cross.gif

Images/not.gif

New.gif

Finally, if you want to change the manifest file of tictactoe. Jar (add some versions and vendor information), you should first prepare the following text documents:

Versioninfo

----------------

Name: tictactoe. Class

Implementation-title: "tictactoe Demo"

Implementation-version: "build57"

Implementation-vendor: "Sun Microsystems, Inc ."

----------------

Then,

Tictactoe> jar Umf versioninfo tictactoe. Jar

Update.

6. Execute the software archived in jar

Three cases:

1) jar archives the applet and is browsed by the browser.

2) jar archives the application and calls it in the command line.

3) jar contains code that will be used for extension

(The first two cases will be described below, 3) See URL: http://java.sun.com/docs/books/tutorial/ext/index.html)

 

For the applet, the following is an example in HTML:

----------------

<APPLET code = tictactoe. Class

Width = 120 Height = 120>

</APPLET>

----------------

If the class is in the jar archive, use the archive parameter:

----------------

Applet code = tictactoe. Class

Archive = "tictactoe. Jar"

Width = 120 Height = 120>

</APPLET>

----------------

If the jar and the HTML are not in the same directory, but in the applets subdirectory of the current directory,

----------------

<APPLET code = tictactoe. Class

Archive = "applets/tictactoe. Jar"

Width = 120 Height = 120>

</APPLET>

----------------

 

For application, it is on the Java version 1 platform.

You can use the JRE tool:

/> JRE-CP app. Jar mainclass

-The CP switch pre-adds app. jar to the system classpath.

 

For Java version 2.

/> JAVA-Jar-File

Note: Java commands earlier than Java version 2 do not support the-jar switch.

Of course, runtime environment must know the entry point of the Program (mainclass ). As mentioned above, this is a manifest task. In manifest:

Main-class: classname

For example:

----------------

Main-class: helloworld

----------------

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.