- Forward


Class Loaders in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

The Need for a Class Loader
Back SMYC Forward
  • The Java Interpreter:
    • "Executes" the byte codes (i.e., the instructions for a hypothetical machine)
  • Obtaining the Byte Codes:
    • The interpreter obtains the byte codes that constitute a class using a class loader
Default and Custom Class Loaders
Back SMYC Forward
  • The Default Class Loader:
    • Assumes that the byte codes are stored in a .class file on the local machine
  • Examples of Custom Class Loaders:
    • Load classes from a remote machine
    • Load encrypted class files
    • Load only classes that have been paid for
Some Details
Back SMYC Forward
  • The Java Interpreter:
    • Actually uses three different class loaders
  • The Class Loaders:
    • Bootstrap Class Loader - loads system classes (normally from rt.jar)
    • Extension Class Loader - loads standard extensions (normally in the jre/lib/ext directory)
    • System/Application Class Loader - loads application classes (normally found in the CLASSPATH)
Desirable Features
Back SMYC Forward
  • Lazy Loading:
    • By default, classes are loaded "on demand" when they are needed to resolve links
    • If necessary, early loading can be used
  • Link-Time Type Safety:
    • Type checks are performed only once, at link time
  • Enhanced Namespaces:
    • A class has a defining loader (i.e., the instance that directly loaded and defined the class)
    • The namespace is specified by the (fully-qualified) name of the class and the defining loader (i.e., one can distinguish classes with the same name but different loaders)
Delegation
Back SMYC Forward
  • An Observation:
    • A .class file might contain a reference to another class that needs to be loaded
  • A Definition:
    • The JVM uses the class loader that defined the referencing class as the initiating loader for the referenced class
  • Delegation:
    • The initiating loader can either load the class itself or delegate to another class loader
    • If another class loader needs to be constructed, the initiaing loader becomes the "delegation parent" of the newly constructed loader (by passing itself to the constructor)
Delegation (cont.)
Back SMYC Forward
  • Starting the Process:
    • The system/application class loader is created
    • The "main class" is loaded
  • Continuing:
    • The "main class" references other classes which must be loaded either by the defining loader or a new class loader (that has the system/application class loader as its "delegation parent")
  • Continuing Further:
    • The newly loaded class may reference other classes which must be loaded and the process continues (creating a "delegation tree" of class loaders)
Discovery
Back SMYC Forward
  • The Relevant Methods:
    • public Class loadClass(String name)
    • protected Class findLoadedClass(String name)
    • protected Class findClass(String name)
  • The Process:
    • loadClass() calls this.findLoadedClass()
    • If this.findLoadedClass() returns null then the loadClass() method in the "delegation parent" is called
    • If the loadClass() method in the "delegation parent" returns null (which means that no class loader in the "delegation tree" could find the class) then this.findClass() is called
There's Always More to Learn
Back -