Jar Files The Basics Outline Create a Jar
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 2
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. 3
– 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. 4
Example • The Tic. Tac. Toe applet demo comprises: – a class file – audio directory: contain sound files – images directory: contain GIF images all in a directory called Tic. Tac. Toe: 5
• In the Tic. Tac. Toe directory, execute jar cvf Tic. Tac. Toe. jar Tic. Tac. Toe. class audio images • Tic. Tac. Toe. jar is placed in the current directory. • The verbose output appears on the next slide. 6
adding: adding: adding: Tic. Tac. Toe. class (in=3825) (out=2222) (deflated 41%) audio/ (in=0) (out=0) (stored 0%) audio/beep. au (in=4032) (out=3572) (deflated 11%) audio/ding. au (in=2566) (out=2055) (deflated 19%) audio/return. au (in=6558) (out=4401) (deflated 32%) audio/yahoo 1. au (in=7834) (out=6985) (deflated 10%) audio/yahoo 2. au (in=7463) (out=4607) (deflated 38%) images/ (in=0) (out=0) (stored 0%) images/cross. gif (in=157) (out=160) (deflated -1%) images/not. gif (in=158) (out=161) (deflated -1%) 7
Using the Wildcard Character Using the wildcard character (*), an equivalent command, in this case, is: jar cvf Tic. Tac. Toe. jar * 8
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 9
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 Tic. Tac. Toe. jar This command’s output appears on the next slide. 10
META-INF/MANIFEST. MF Tic. Tac. Toe. class audio/beep. au audio/ding. au audio/return. au audio/yahoo 1. au audio/yahoo 2. au images/cross. gif images/not. gif 11
jar tvf Tic. Tac. Toe. jar displays 256 Mon Apr 20 10: 50: 28 PDT 1998 META-INF/MANIFEST. MF 3885 Mon Apr 20 10: 49: 50 PDT 1998 Tic. Tac. Toe. 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/yahoo 1. au 7463 Wed Apr 15 16: 39: 32 PDT 1998 audio/yahoo 2. 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 12
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 13
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 Tic. Tac. Toe. jar Extracts the JAR’s files into the current directory. 14
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 15
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 manifestaddition file contains the line: Main-Class: classname where classname must omit the. class extension E. g. , Tic. Tac. Toe (not Tic. Tac. Toe. class) 16
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 17
Running JAR-Packaged Software Applets Packaged in JAR Files <applet code=Tic. Tac. Toe. class archive="Tic. Tac. Toe. 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. 18
• JAR Files as Applications - 1. 2 platform java -jar jar-file The JAR’s manifest must designate the main-class. • Example (continued) java -jar Tic. Tac. Toe. jar Based on the Tic. Tac. Toe. jar with the manifest that designates Tic. Tac. Toe as the main-class. 19
- Slides: 19