The jar UtilityThe Java archive (JAR) file format enables you to bundle multiple files into a single archive file, much the same way you can bundle files by using the tar utility. Typically, a JAR file contains the class files and auxiliary resources associated with Java applets and applications. The benefits of using the JAR file format include the following:
The jar command is similar to the tar command in that it packages several files into a single file, but it also compresses the resulting file. It is a Java application that combines multiple files into a single JAR file. It is also a general-purpose archiving and compression tool that is based on Zip and the ZLIB compression format. The jar command was originally created so that Java programmers could download multiple files with one request rather than having to issue a download request for each separate file. jar is standard with the Solaris 10 operating system, and it is also available on any system that has a Java Virtual Machine (JVM) installed. This is the syntax for the jar command: jar cf <jar-file> <input-file(s)> Table 7.17 describes the options and arguments used with the jar command.
You use the following to create a JAR file: jar cf <jar-file> <input-file(s)> You use the following to view the contents of a JAR file: jar tf <jar-file> You use the following to extract the contents of a JAR file: jar xf <jar-file> You use the following to extract specific files from a JAR file: jar xf <jar-file> <archived-file(s)> Here's an example of how to use jar to compress files located within two different directories. JAR files are packaged with the Zip file format, so you can use them for Zip-like tasks, such as lossless data compression, archiving, decompression, and archive unpacking. To package the audio and images directories into a single JAR file named files.jar in your default home directory, you would run the following command from inside the /export/home/bcalkins directory: jar cvf ~/files.jar files.class audio images The audio and images arguments represent directories, so the JAR tool recursively places them and their contents in the JAR file. The generated JAR file files.jar is placed in the user's home directory. Because the command used the v option for verbose output, you see something similar to this output when you run the command: adding: files.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%) You can see from this output that the JAR file files.jar is compressed. The JAR tool compresses files by default. You can turn off the compression feature by using the 0 option; in that case, the command looks like this: jar cvf0 files.jar files.class audio images |