Creating and Using Extensions |
Installed extensions are JAR files in the lib/ext directory of the Java Runtime Environment (JRE) software. As its name implies, the JRE is the runtime portion of the Java Development Kit containing the platform's core API but without development tools such as compilers and debuggers. The JRE is available either by itself or as part of the Java Development Kit.In the 1.2 platform, the JRE is a strict subset of the JDKTM software. The JDK 1.2 software directory tree looks like this:
JRE 1.2 consists of those directories within the highlighted box in the diagram. Whether your JRE is stand-alone or part of the JDK software, any JAR file in the JRE's lib/ext directory is automatically treated by the runtime environment as an extension.
A Simple Example
Let's create a simple installed extension. Our extension consists of one class, RectangleArea, that computes the areas of rectangles:This class has a single method, area, that takes an instance of java.awt.Rectangle and returns the rectangle's area.public final class RectangleArea { public static int area(java.awt.Rectangle r) { return r.width * r.height; } }Suppose you want to test RectangleArea with this application called
AreaApp
:This application instantiates a 10 x 5 rectangle, and then prints out the rectangle's area using the RectangleArea.area method.import java.awt.*; public class AreaApp { public static void main(String[] args) { int width = 10; int height = 5; Rectangle r = new Rectangle(width, height); System.out.println("The rectangle's area is " + RectangleArea.area(r)); } }
Running AreaApp Without the Extension Mechanism
Let's first review how you would run theAreaApp
application without using the extension mechanism. We'll assume that the RectangleArea class is bundled in a JAR file named area.jar.The RectangleArea class is not part of the Java platform, of course, so you would need to place the area.jar file on the classpath in order to run
AreaApp
without getting a runtime exception. If area.jar was in the directory /home/user, for example, you could use this command:The classpath specified in this command contains both the current directory, containing AreaApp.class, and the path to the JAR file containing the RectangleArea package. Running this command results in the desired output:java -classpath .:/home/user/area.jar AreaAppThe rectangle's area is 50
Running AreaApp Using the Extension Mechanism
Now let's look at how you would runAreaApp
by using the RectangleArea class as an extension.To make the RectangleArea class into an extension, you place the file area.jar in the lib/ext directory of the JRE. Doing so automatically gives the RectangleArea the status of being an installed extension.
With area.jar installed as an extension, you can run
AreaApp
without needing to specify the classpath:java AreaAppBecause you're using area.jar as an installed extension, the runtime environment will be able to find and load the
RectangleArea
class even though you haven't specified it on the classpath. Similarly, any applet or application being run by any user on your system would be able to find and use the RectangleArea class.
Creating and Using Extensions |