The JRE is a strict subset of the JDK&trade software. A subset of the JDK software directory tree looks like this:
The JRE 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 lib/ext of the JRE directory is automatically treated by the runtime environment as an extension.
Since installed extensions extend the platform's core API, use them judiciously. They are rarely appropriate for interfaces used by a single, or small set of applications.
Furthermore, since the symbols defined by installed extensions will be visible in all Java processes, care should be taken to ensure that all visible symbols follow the appropriate "reverse domain name" and "class hierarchy" conventions. For example, com.mycompany.MyClass.
As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system. Prior to Java 6, the value of java.ext.dirs referred to a single directory, but as of Java 6 it is a list of directories (like CLASSPATH) that specifies the locations in which extensions are searched for. The first element of the path is always the lib/ext directory of the JRE. The second element is a directory outside of the JRE. This other location allows extension JAR files to be installed once and used by several JREs installed on that system. The location varies depending on the operating system:
Note that an installed extension placed in one of the above directories extends the platform of each of the JREs (Java 6 or later) on that system.
public final class RectangleArea { public static int area(java.awt.Rectangle r) { return r.width * r.height; } }
Suppose that you want to test RectangleArea with an application
called AreaApp
:
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)); } }
AreaApp
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
class path 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:
java -classpath .:/home/user/area.jar AreaApp
The rectangle's area is 50
AreaApp
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 class path:
java AreaApp
Because you're using area.jar as an installed extension, the
runtime environment will be able to find and to load the
RectangleArea
class even though you haven't specified it on
the class path. Similarly, any applet or application being run by any user
on your system would be able to find and use the RectangleArea class.
If there are multiple JREs (Java 6 or later) installed on a system and want the RectangleArea class to be available as an extension to all of them, instead of installing it in the lib/ext directory of a particular JRE, install it in the system-wide location. For example, on system running Linux, install area.jar in the directory /usr/java/packages/lib/ext. Then AreaApp can run using different JREs that are installed on that system, for example if different browsers are configured to use different JREs.