190 likes | 409 Views
Jar Files. The Basics. Outline. Create a Jar Viewing the Contents of a JAR File Extracting the Contents of a JAR File Modifying a Manifest File Running JAR-Packaged Software. Create a Jar. The basic format of the jar command: jar cf jar-file input-file(s)
E N D
Jar Files The Basics
Outline • Create a Jar • Viewing the Contents of a JAR File • Extracting the Contents of a JAR File • Modifying a Manifest File • Running JAR-Packaged Software
Create a Jar The basic format of the jar command: jar cf jar-file input-file(s) • The c option says: create a JAR. • The f option says: create it as a file. • jar-file: the file’s name. By convention, JAR filenames have a .jar extension. • input-file(s): a space-separated file list to be archived. • An input-file can contain the wildcard * symbol. • A directory’s contents are added recursively.
The generated JAR resides, compressed, in the current directory. • It also generates a default manifest file. • Some other options: • v: Writes each added file’s name to stdout. • 0: (zero) Don’t compress the JAR file.
Example • The TicTacToe applet demo comprises: • a class file • audio directory: contain sound files • images directory: contain GIF images all in a directory called TicTacToe:
In the TicTacToe directory, execute jar cvf TicTacToe.jar TicTacToe.class audio images • TicTacToe.jar is placed in the current directory. • The verbose output appears on the next slide.
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%)
Using the Wildcard Character Using the wildcard character (*), an equivalent command, in this case, is: jar cvf TicTacToe.jar *
Outline • Create a Jar • Viewing the Contents of a JAR File • Extracting the Contents of a JAR File • Modifying a Manifest File • Running JAR-Packaged Software
Viewing the Contents of a JAR File • To view a jar, execute: jar tf jar-file The t option: view the JAR’s table of contents. • Example (continued) jar tf TicTacToe.jar This command’s output appears on the next slide.
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
jar tvf TicTacToe.jar displays 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
Outline • Create a Jar • Viewing the Contents of a JAR File • Extracting the Contents of a JAR File • Modifying a Manifest File • Running JAR-Packaged Software
Extracting a JAR’s Contents • To extract the contents of a JAR, execute: jar xf jar-file The x option: extract files from jar-file. • This overwrites existing files having the same pathname as the extracted files. • Example (continued) jar xf TicTacToe.jar Extracts the JAR’s files into the current directory.
Outline • Create a Jar • Viewing the Contents of a JAR File • Extracting the Contents of a JAR File • Modifying a Manifest File • Running JAR-Packaged Software
Modifying a Manifest File • To create a JAR & add manifest info: jar cmf manifest-addition jar-file input-file(s) The m option: merge a manifest file into the JAR’s. manifest-addition: the manifest file’s name. • To designate the main class in the JAR, the manifest-addition file contains the line: Main-Class: classname where classname must omit the .class extension E.g., TicTacToe (not TicTacToe.class)
Outline • Create a Jar • Viewing the Contents of a JAR File • Extracting the Contents of a JAR File • Modifying a Manifest File • Running JAR-Packaged Software
Running JAR-Packaged Software Applets Packaged in JAR Files <applet code=TicTacToe.class archive="TicTacToe.jar" width=120 height=120> </applet> • The archive parameter specifies the relative path to the JAR file. • This example assumes that the JAR & HTML files are in the same directory.
JAR Files as Applications - 1.2 platform java -jar jar-file The JAR’s manifest must designate the main-class. • Example (continued) java -jar TicTacToe.jar Based on the TicTacToe.jar with the manifest that designates TicTacToe as the main-class.