Java Extension Mechanism

The extension mechanism provides a standard, scalable way to make custom APIs available to all applications running on the Java platform. The extension mechanism enables the runtime environment to find and load extension classes without the extension classes having to be named on the class path. In that respect, extension classes are similar to the Java platform's core classes.

Any set of packages or classes can easily be made to play the role of an extension. The first step in turning a set of classes into an extension is to bundle them in a JAR file. Once that's done, you can turn the software into an extension in two ways:

  • By placing the JAR file in jdk1.x/jre/lib/ext dir of JVM, in which case it's called an installed extension.
  • By referencing the JAR file in a specified way from the manifest of the another JAR file, in which case it's called a download extension. A JAR file's manifest can contain headers (Class-Path or Extension-List) that refer to one or more download extensions.

The value of java.ext.dirs 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:

Solaris™ Operating System: /usr/jdk/packages/lib/ext
Linux: /usr/java/packages/lib/ext
Microsoft Windows: %SystemRoot%\Sun\Java\lib\ext

Extension class loading

When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order:

  1. Bootstrap classes: the runtime classes in rt.jar, internationalization classes in i18n.jar, and others.
  2. Installed extensions
  3. The class path: classes, including classes in JAR files, on paths specified by the system property java.class.path. If a JAR file on the class path has a manifest with the Class-Path attribute, JAR files specified by the Class-Path attribute will be searched also. By default, the java.class.path property's value is ., the current directory. You can change the value by using the -classpath or -cp command-line options, or setting the CLASSPATH environment variable. The command-line options override the setting of the CLASSPATH environment variable.

The Java platform uses a delegation model for loading classes. The basic idea is that every class loader has a "parent" class loader. When loading a class, a class loader first "delegates" the search for the class to its parent class loader before attempting to find the class itself.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License