Packaging an applet into a JAR file
Packaging an applet into a JAR
file
An important use of the JAR utility is to optimize
applet loading. In Java 1.0, people tended to try to cram all their code into a
single applet class so the client would need only a single server hit to
download the applet code. Not only did this result in messy, hard to read (and
maintain) programs, but the .class file was still uncompressed so
downloading wasn’t as fast as it could have been.
JAR files solve the problem by compressing all of your
.class files into a single file that is downloaded by the browser. Now
you can create the right design without worrying about how many .class
files it will generate, and the user will get a much faster download
time.
Consider TicTacToe.java. It looks like a single
class, but in fact it contains five inner classes, so that’s six in all.
Once you’ve compiled the program, you package it into a JAR file with the
line:
jar
cf TicTacToe.jar
*.class
This assumes that the only .class files in the
current directory are the ones from TicTacToe.java (otherwise
you’ll get extra baggage).
Now you can create an HTML page with the new
archive tag to indicate the name of the JAR file. Here is the tag using
the old form of the HTML tag, as an illustration:
<head><title>TicTacToe
Example Applet
</title></head>
<body>
<applet
code=TicTacToe.class
archive=TicTacToe.jar
width=200 height=100>
</applet>
</body>
You’ll
need to put it into the
new
(messy, complicated) form shown earlier
|