JAR Files

Java 1.1 added support for Java ARchive files, JAR files for short. JAR files bundle the many different classes, images, and sound files an applet requires into a single file. It is generally faster for a web browser to download one JAR file than to download the individual files the archive contains, since only one HTTP connection is required. An applet stored in a JAR file, instead of as merely loose .class files, is embedded in a web page with an <applet> tag with an archive attribute pointing to the JAR file. For example:

<applet code=NavigationMenu archive="NavigationMenu.jar" width=400 height=80>

The code attribute still says that the main class of this applet is called NavigationMenu. However, a Java 1.1 web browser, rather than asking the web server for the file NavigationMenu.class as a Java 1.0 web browser would, asks the web server for the file NavigationMenu.jar. Then the browser looks inside NavigationMenu.jar to find the file NavigationMenu.class. Only if it doesn’t find NavigationMenu.class inside NavigationMenu.jar does it then go back to the web server and ask for NavigationMenu.class. Now suppose the NavigationMenu applet tries to load an image called menu.gif. The applet will look for this file inside the JAR archive too. It only has to make a new connection to the web server if it can’t find menu.gif in the archive.

Sun wisely decided not to attempt to define a new file format for JAR files. Instead, they stuck with the tried-and-true zip format. ...

Get Java I/O now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.