Java Reference
In-Depth Information
is really being implemented. Use a class instead and then statically import the
class.
5.6 JAR files
A useful technique for organizing classes and packages is to combine them into
a file called a JAR or Java Archive file. JAR files (often referred to as “jar” files
but we use the uppercase JAR in this topic) are based on the ZIP archiving and
compression system. JARs provide a number of advantages:
Loading a single large JAR file over the network instead of several small class files
reduces the overhead that occurs for each network transfer.
Compression, which is the default but may be turned off, also helps for faster loading
since a compressed JAR file is usually significantly smaller than the sum of all the class
files in the JAR.
Internally the ZIP format maintains the directory structure of the packages.
The applet
TestABCApplet
discussed above (section 5.3.2) needed classes
from two packages. Here we use the
jar
tool (provided with the SDK) to cre-
ate a JAR file that holds the
TestABCApplet
class file and the
mypack
and
mypack.extrapack
packages. The command line and output here shows the
jar
tool in action:
c:
\
...
\
myApps>jar -cvf TestABCApplet.jar TestABCApplet.class
mypack
added manifest
adding: TestABCApplet.class(in = 1109) (out= 648) (deflated 41%)
adding: mypack/(in
=
0) (out
=
0) (stored 0%)
adding: mypack/extrapack/(in = 0) (out= 0) (stored 0%)
adding: mypack/extrapack/TestC.class(in = 250) (out= 203)
(deflated 18%)
adding: mypack/extrapack/TestC.java(in = 129) (out= 93)
(deflated 27%)
adding: mypack/TestA.class(in
=
237) (out
=
194) (deflated 18%)
adding: mypack/TestA.java(in = 111) (out= 81) (deflated 27%)
adding: mypack/TestB.class(in
=
237) (out
=
192) (deflated 18%)
adding: mypack/TestB.java(in = 110) (out= 79) (deflated 28%)
C:
\
...
\
myApps
>
The
-c
option means to “create” the JAR file; the
v
option means to use “verbose”
output; and the
f
option specifies the file name of the created file. JAR files should
use the
“.jar
„
filename extension. We see that the command above adds the
applet class file
TestABCApplet.class
and the classes in the packages to
the archive, compresses them, and maintains the package directory structure.
Search WWH ::
Custom Search