You can provide all of an applet's class and data files in a single Java archive (JAR) file, using the JDKjar
tool and the newARCHIVE
attribute of the<APPLET>
. Using JAR files can improve an applet's performance in two ways:
- It can drastically reduce applet loading time.
Each file that a browser brings over the network, whether the file contains ASCII text or Java bytecodes or image data, requires its own HTTP connection. Usually, the time spent setting up HTTP connections dwarfs the time spent performing the actual transfer. By packaging several of an applet's files in a single file, you reduce the number of HTTP connections the browser needs to make when running the applet, which can save many seconds of transfer time.
- It can enable users behind firewalls to use the applet.
Many firewalls work in a way that doesn't permit browsers to get additional files for an applet supplied by a server outside the firewall. By putting all of the applet's class and data files in a single JAR file, you avoid this problem. [CHECK!]To tell a browser about JAR files, use the new
ARCHIVE
attribute of the<APPLET>
tag.< APPLET ... [ARCHIVE = JARFile1, JARFile2, ...] ... >Here's an example of creating and specifying a JAR file. Assume you have an applet that consists of two class files and an image file. You can create a JAR file that contains all three files. For example, on Solaris using the JDK, you might use this command:
- ARCHIVE = JARFile1, JARFile2, ...
- This optional attribute specifies one or more Java archive (JAR) files associated with the applet. The archive files should be in the same directory the applet's class files would be. To specify that the archive file isn't in the same directory as the document containing the
<APPLET>
tag, specify theCODEBASE
attribute.Whenever a browser needs to load a file needed by an applet, the browser looks in the applet's JAR files first. If it doesn't find the file in the first JAR file specified with
ARCHIVE
, it looks any other specified JAR files, in the order they were specified. If the browser fails to find the file in a JAR file, then it looks in the applet's base directory. [Do we really guarantee that the JAR file specification order is the order in which the browser will look?]
You could then specify the JAR file to the browser using thisjar cvf MyApplet.jar AnAppletSubclass.class Helper.class aPic.gif<APPLET>
tag:<APPLET ARCHIVE = MyApplet.jar CODE = AnAppletSubclass.class WIDTH = 300 HEIGHT = 100 > </APPLET>Here's an example of a more complex
<APPLET>
tag that uses theARCHIVE
attribute:<APPLET CODEBASE="example/" ARCHIVE="AppletButton.jar, IntlWindow.jar, Media.jar" CODE="AppletButton.class" WIDTH=350 height=60 ALT="Your browser understands the applet tag but isn't displaying any applet."> <PARAM NAME="windowWidth" VALUE=500> <PARAM NAME="windowHeight" VALUE=130> Since you can't run the applet, here's a picture of it: <br> <IMG SRC="images/AroundTheWorld_trans.gif" WIDTH=316 HEIGHT=411> </APPLET>