to.. ELA Web Portal to.. Java Notes to.. ELA Notes

ELA JAR NOTES

from "JAR Files:" Sun Java Tutorial

JAR-file operations
Operation Command
Create JAR file
c (create); f (file)
* (wild card for all files/subfolders)
jar cf jar-file input-file(s)
or
jar cf jar-file *
View contents
t (table)
jar tf jar-file
Extract contents
x (extract)
jar xf jar-file
Extract specific files jar xf jar-file archived-file(s)
Run application from JAR file
(version 1.2 -- requires Main-Class
manifest header)
java -jar app.jar
Run application from JAR file
(version 1.1)
jre -cp app.jar MainClass
Invoke an applet in a JAR file
<applet code=AppletClassName.class
        archive="JarFileName.jar"
        width=width height=height>
</applet>


Creating a JAR File
Option Description
v verbose output - name each file as it's added to the JAR file.
0 (zero) no compression
M Do not produce default manifest file.
m Use manifest file information. Format:
jar cmf existing-manifest-file jar-file input-file(s)
-C Locate (change) directories and flatten directory heirarchy (v1.2 only)
See example 2 below.

Example 1)

jar cvmf mani.txt MyJar.jar *

Where...
c create (jar file MyJar.jar)
m manifest file (mani.txt)
f file
* wildcard jar all files/folders (preserves directory hierarchy)

Example 2)

Using the JDK v1.2 Jar tool -C creates a JAR file that does not preserve relative paths of archived files. To put all files from directories Dir1 and Dir2 (under the current directory) into MyJar.jar with these files on the top level and no directory hierarchy...

jar cf MyJar.jar -C Dir1 . -C Dir2 .

Where -C  directs the Jar tool to the Dir1 and Dir2 directories, and the 
period " . " directs flat archive of all files in Dir1 and Dir2, respectively, 
without subdirectory structure. 

Contrast the above with...

jar cf MyJar.jar Dir1 Dir2

Which directs the Jar tool to the Dir1 and Dir2 directories for archiving 
of all files in Dir1 and Dir2 while preserving directory heirarchy.