Since download extensions that use the Class-Path headers are simpler, lets consider them first. Assume for example that a.jar and b.jar are two JAR files in the same directory, and that the manifest of a.jar contains this header:
Class-Path: b.jar
There's nothing special about the classes that are playing the role of a download extension. They are treated as extensions solely because they're referenced by the manifest of some other JAR file.
To get a better understanding of how download extensions work, let's create one and put it to use.
RectangleArea
class of the previous
section:
public final class RectangleArea { public static int area(java.awt.Rectangle r) { return r.width * r.height; } }
If you want to be able to use the RectangleArea class from an
applet, the situation is a little different. Suppose, for example,
that you have an applet, AreaApplet
, that makes use of class
RectangleArea:
import java.applet.Applet; import java.awt.*; public class AreaApplet extends Applet { Rectangle r; public void init() { int width = 10; int height = 5; r = new Rectangle(width, height); } public void paint(Graphics g) { g.drawString("The rectangle's area is " + RectangleArea.area(r), 10, 10); } }
However, you can't assume that everyone who downloads and uses your applet is going to have the RectangleArea class available on their system, as an installed extension or otherwise. One way around that problem is to make the RectangleArea class available from the server side, and you can do that by using it as a download extension.
To see how that's done, let's assume that you've bundled
AreaApplet
in a
JAR file called AreaApplet.jar and that the class
RectangleArea is bundled in RectangleArea.jar. In order for
RectangleArea.jar to be treated as a download extension,
RectangleArea.jar must be listed in the
Class-Path header in AreaApplet.jar's manifest.
AreaApplet.jar's manifest might look like this, for example:
Manifest-Version: 1.0 Class-Path: RectangleArea.jar
Class-Path: area.jar servlet.jar images/
Note that only one Class-Path header is allowed in a manifest file, and that each line in a manifest must be no more than 72 characters long. If you need to specify more class path entries than will fit on one line, you can extend them onto subsequent continuation lines. Begin each continuation line with two spaces. For example:
Class-Path: area.jar servlet.jar monitor.jar datasource.jar provider.jar gui.jar
Download extensions can be "daisy chained", meaning that the manifest of one download extension can have a Class-Path header that refers to a second extension, which can refer to a third extension, and so on.
Since this mechanism extends the platform's core API, its use should be judiciously applied. It is rarely appropriate for interfaces used by a single, or small set of applications. All visible symbols should follow reverse domain name and class hierarchy conventions.
The basic requirements are that both the applet and the extensions it uses provide version information in their manifests, and that they be signed. The version information allows Java Plug-in to ensure that the extension code has the version expected by the applet. For example, the AreaApplet could specify an areatest extension in its manifest:
Manifest-Version: 1.0 Extension-List: areatest areatest-Extension-Name: area areatest-Specification-Version: 1.1 areatest-Implementation-Version: 1.1.2 areatest-Implementation-Vendor-Id: com.example areatest-Implementation-URL: http://www.example.com/test/area.jar
Manifest-Version: 1.0 Extension-Name: area Specification-Vendor: Example Tech, Inc Specification-Version: 1.1 Implementation-Vendor-Id: com.example Implementation-Vendor: Example Tech, Inc Implementation-Version: 1.1.2
keytool -genkey -dname "cn=Fred" -alias test -validity 180
jarsigner AreaApplet.jar test jarsigner area.jar test
Here is AreaDemo.html, which loads the applet and causes the extension code to be downloaded and installed:
<html> <body> <applet code="AreaApplet.class" archive="AreaApplet.jar"/> </body> </html>
After restarting the web browser and load the same web page, only the dialog about the applet's signer is presented, because area.jar is already installed. This is also true if AreaDemo.html is opened in a different web browser (assuming both browsers use the same JRE).
For more information about installing download extensions, please see Deploying Java Extensions. A more detailed example is at Deploying Java Media Framework as Java Extension.