Creating and Using Extensions |
The extension framework makes use of the new class loading mechanism in version 1.2 of the JavaTM platform. When the runtime environment needs to load a new class, it looks for the class in the following locations, in order:
- bootstrap classes -- the runtime classes in rt.jar and internationalization classes in i18n.jar.
- installed extensions -- classes in JAR files in the lib/ext directory of the JRE.
- the class path -- classes on paths specified by the system property java.class.path. By default, this property's value is ".", the current directory. You can change the value by setting the
CLASSPATH
environment variable or by using the -classpath or -cp command-line options. These command-line options override the setting of theCLASSPATH
environment variable. Note that in version 1.2 of the Java software, java.class.path no longer includes the bootstrap classes in rt.jar and i18n.jar.- download extensions -- any JAR files specified in the
Class-Path
headers of installed-extension JAR files, JAR files on the class path, or previously loaded download-extension JAR files.The precedence list tells you, for example, that the class path is searched only if a class to be loaded hasn't been found among the classes in rt.jar, i18n.jar or the installed extensions. You can also see that download extensions have the lowest priority. Classes in download extensions will be loaded only if the classes haven't first been found elsewhere.
Unless your software instantiates its own class loaders for special purposes, you don't really need to know much more than to keep this precedence list in mind. In particular, you should be aware of any classname conflicts that might be present. For example, if you list a class on the class path, you'll get unexpected results if the runtime environment instead loads another class of the same name that it found in an installed extension.
The 1.2 Class Loading Mechanism
Version 1.2 of the Java platform uses a new 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.Here are some highlights of the class loading API:
- Constructors in java.lang.ClassLoader and its subclasses allow you to specify a parent when you instantiate a new class loader. If you don't explicitly specify a parent, the virtual machine's system class loader will be assigned as the default parent.
- The loadClass method in ClassLoader performs these tasks, in order, when called to load a class:
- If a class has already been loaded, it returns it.
- Otherwise, it delegates the search for the new class to the parent class loader.
- If the parent class loader doesn't find the class, loadClass calls the method findClass to find and load the class.
- The findClass method of ClassLoader searches for the class in the current class loader if the class wasn't found by the parent class loader. You will probably want to override this method when you instantiate a class loader subclass in your application.
- The class java.net.URLClassLoader has been added to the core API. This class serves as the basic class loader for extensions and other JAR files. It overrides the findClass method of java.lang.ClassLoader to search one or more specified URLs for classes and resources.
To see an example application that uses some of the new API as it relates to JAR files, see the JAR File Format trail in this tutorial.
Changes to the java Command
The version 1.2 platform's class loading mechanism is reflected in some changes to the java command. Here's a summary of the changes:
- Version 1.2 of the JRE now includes the same Java interpreter, invoked with the java command, as is in the JDKTM 1.2 software. This tool replaces the old jre tool in JRE 1.1.
- In version 1.2 of the java tool, the -classpath option is a shorthand way to set the java.class.path property. Formerly, the -classpath option was used to override the search path for system classes.
- The -cp option, formerly part of the jre utility, has been added as an option to the java command. The -cp and -classpath options are equivalent.
- The -jar option has been added for running applications that are packaged in JAR files. For a description and examples of this new option, see the JAR File Format trail in this tutorial.
Creating and Using Extensions |