Home » 2015 » January » 18 » The JAR archiving tool in Linux

11:48 AM
The JAR archiving tool in Linux

JAR, a Java Archive Tool is a command line tool in linux used to combine multiple files into a single JAR archive file.


Format
       Create jar file
          jar c[v0Mmfe] [manifest] [jarfile] [entrypoint] [-C dir] inputfiles [-Joption]

       Update jar file
          jar u[v0Mmfe] [manifest] [jarfile] [entrypoint] [-C dir] inputfiles [-Joption]

       Extract jar file
          jar x[vf] [jarfile] [inputfiles] [-Joption]

       List table of contents of jar file
          jar t[vf] [jarfile] [inputfiles] [-Joption]

       Add index to jar file
          jar i jarfile [-Joption]

       where:   cuxtiv0Mmfe are the options that control the jar command.

      jarfile
             Jar  file  to  be  created  (c),  updated (u), extracted (x), or have its table of contents viewed (t). The -f option and filename jarfile are a pair -- if either is present, they must both appear. Note that omitting f and jarfile  accepts  a  "jar  file"  from            standard input (for x and t) or sends the "jar file" to standard output (for c and u).

    inputfiles
             Files or directories, separated by spaces, to be combined into jarfile (for c and u), or to be extracted (for x) or listed (for t) from jarfile. All directories are processed recursively. The files are compressed unless option 0 (zero) is used.

    manifest
             Pre-existing manifest file whose name: value pairs are to be included in MANIFEST.MF in the jar file. The -m option  and  filename manifest are a pair -- if either is present, they must both appear. The letters m, f and e must appear in the same order that manifest, jarfile, entrypoint appear.



DESCRIPTION
       The  jar  tool combines multiple files into a single JAR archive file. jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB @      http://www.gzip.org/zlib/ compression format.

However, jar was designed mainly package java applets or applications into a single       archive.

When the components of an applet or application (files, images and sounds) are combined into a single archive, they can be downloaded by a java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece.

This dramatically improves download times. jar also compresses files and so further improves download time. In addition, it allows individual entries in a file to be signed by the applet author so that their origin can be authenticated.

The syntax for the jar tool is almost identical to the syntax for the tar command.


Examples:

1.Combining multiple files into a jar.

          % jar cf myFile.jar *.class

[blross6@rhevh1 testJava]$ ls
Sample.class  Sample.java  Test.class  Test.java

 

[blross6@rhevh1 testJava]$ jar -cvf myjar.jar *
added manifest
adding: Sample.class(in = 419) (out= 289)(deflated 31%)
adding: Sample.java(in = 99) (out= 94)(deflated 5%)
adding: Test.class(in = 415) (out= 287)(deflated 30%)
adding: Test.java(in = 101) (out= 94)(deflated 6%)

 

[blross6@rhevh1 testJava]$ ls
myjar.jar  Sample.class  Sample.java  Test.class  Test.java

 

       In this example, all the class files or all the files (if given *) in the current directory are placed into the file named myjar.jar.

The jar tool automatically generates a manifest file entry named META-INF/MANIFEST.MF.

It is always the first entry in the jar file. The manifest file declares meta-information about the archive, and stores that data as name : value pairs.

[blross6@rhevh1 testJava]$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.6.0_33 (Sun Microsystems Inc.)

 

       If a jar file should include name : value pairs contained in an existing manifest file, specify that file using the -m option:

          % jar cmf myManifestFile myjar.jar *.class

       An existing manifest file must end with a new line character.  jar does not parse the last line of a manifest file if it does not end
       with a new line character.


 Note:- An existing manifest file must end with a new line character.  jar does not parse the last line of a manifest file if it does not end with a new line character.

Note:  A jar command that specifies cfm on the command line instead of cmf (the order of the m and -f options are reversed), the jar command line must specify the name of the jar archive first, followed by the name of the manifest file:

          $ jar cfm myFile.jar myManifestFile *.class

       The manifest is in a text format inspired by RFC822 ASCII format, so it is easy to view and process manifest-file contents.


      

2.To extract the files from a jar file, use x:

          % jar xf myFile.jar

[blross6@rhevh1 testJava]$ jar -xvf myjar.jar
  created: META-INF/
 inflated: META-INF/MANIFEST.MF
 inflated: Sample.class
 inflated: Sample.java
 inflated: Test.class
 inflated: Test.java
[blross6@rhevh1 testJava]$ ls
META-INF  myjar.jar  Sample.class  Sample.java  Test.class  Test.java

      

3.To extract individual files from a jar file, supply their filenames:

          % jar xf myjar.jar Test.java

[blross6@rhevh1 testJava]$ jar -xvf myjar.jar Test.java
 inflated: Test.java

      


Beginning with version 1.3 of the Java 2 SDK, the jar utility supports JarIndex @       http://ccc.sfbay/guide/jar/jar.html#JAR%20Index, which allows application class loaders to load classes more efficiently from jar files.
       If an application or applet is bundled into multiple jar files,  only the necessary jar files will be downloaded and opened to load classes.

This performance optimization is enabled by running jar with the -ioption. It will generate package location information for the specified main jar file and all the jar files it depends on, which need to be specified in the Class-Path attribute of the main jar files manifest.

          $ jar i main.jar

       In this example, an INDEX.LIST file is inserted into the META-INF directory of main.jar. The application class loader uses the information stored in this file for efficient class loading.  
 


OPTIONS
       c  Creates a new archive file named jarfile (if f is specified) or to standard output (if f and jarfile are omitted). Add to it the files and directories specified by inputfiles.

       u  Updates an existing file jarfile (when f is specified) by adding to it files and directories specified by inputfiles. For example:

          jar uf myjar.jar Test2.class

[blross6@rhevh1 testJava]$ jar uvf myjar.jar Second.java
adding: Second.java(in = 85) (out= 84)(deflated 1%)

       would add the file Test2.class to the existing jar file foo.jar. See the output of t option below to check that the new file is addded to jar file.

The -u option can also update the manifest entry, as given by this example:

          jar umf manifest foo.jar

       updates the foo.jar manifest with the name : value pairs in manifest.

       x  Extracts files and directories from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is
          specified, only those specified files and directories are extracted. Otherwise, all files and directories are extracted. The time and
          date of the extracted files are those given in the archive.

       t  Lists the table of contents from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is specified, only those specified files and directories are listed. Otherwise, all files and directories are listed.

[blross6@rhevh1 testJava]$ jar -tvf myjar.jar
     0 Sun Jan 18 12:06:34 IST 2015 META-INF/
    71 Sun Jan 18 12:06:34 IST 2015 META-INF/MANIFEST.MF
   419 Sun Jan 18 12:04:14 IST 2015 Sample.class
    99 Sun Jan 18 12:04:08 IST 2015 Sample.java
   415 Sun Jan 18 12:01:50 IST 2015 Test.class
   101 Tue Jan 13 17:29:24 IST 2015 Test.java
    85 Sun Jan 18 12:35:58 IST 2015 Second.java

 

       i  Generate index information for the specified jarfile and its dependent jar files. For example:

          jar i foo.jar

 
 

Category: Open System-Linux | Views: 1077 | Added by: shanky | Tags: Create archive using jar in Linux, jar, java archive tool, jar command in Linux, jar utility in Linux | Rating: 5.0/1

Related blogs


You may also like to see:


[2015-03-18][Open System-Linux]
What is runlevel in Linux?
[2014-01-08][Open System-Linux]
SSH Login Setup
[2014-10-26][Open System-Linux]
XMLLINT command in linux : a validating XML parser
[2016-12-11][Open System-Linux]
Some great usage and example of find command in Linux
[2014-03-13][Open System-Linux]
crontab: A command in linux to automatically start/repeat a process at certian time and interval

Total comments: 0
avatar