ClassLoader In Java How And Why

Motivation

Applications written in statically compiled programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. The process of combining the code into an executable native code is called linking - the merging of separately compiled code with shared library code to create an executable application. This is different in dynamically compiled programming languages such as Java. In Java, the .class files generated by the Java compiler remain as-is until loaded into the Java Virtual Machine (JVM) -- in other words, the linking process is performed by the JVM at runtime. Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.

When a Java application is launched, the first class to run (or the entry point into the application) is the one with public static void method called main(). This class usually has references to other classes, and all attempts to load the referenced classes are carried out by the class loader.

To get a feeling of this recursive class loading as well as the class loading idea in general, consider the following simple class:

publicclassHelloApp{
   
publicstaticvoid main(String argv[]){
     
System.out.println("Aloha! Hello and Bye");
   
}
}

If you run this class specifying the -verbose:class command-line option, so that it prints what classes are being loaded, you will get an output that looks as follows. Note that this is just a partial output since the list is too long to show here.

prmpt>java -verbose:classHelloApp



[Opened C:\ProgramFiles\Java\jre1.5.0\lib\rt.jar]
[Opened C:\ProgramFiles\Java\jre1.5.0\lib\jsse.jar]
[Opened C:\ProgramFiles\Java\jre1.5.0\lib\jce.jar]
[Opened C:\ProgramFiles\Java\jre1.5.0\lib\charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
.
.
.
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[LoadedHelloApp from file:/C:/classes/]
Aloha!Hello and Bye
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]

As you can see, the Java runtime classes required by the application class (HelloApp) are loaded first.

Class Loaders in the Java 2 Platform

The Java programming language keeps evolving to make the life of applications developers easier everyday. This is done by providing APIs that simplify your life by allowing you to concentrate on business logic rather than implementation details of fundamental mechanisms. This is evident by the recent change of J2SE 1.5 to J2SE 5.0 in order to reflect the maturity of the Java platform.

As of JDK 1.2, a bootstrap class loader that is built into the JVM is responsible for loading the classes of the Java runtime. This class loader only loads classes that are found in the boot classpath, and since these are trusted classes, the validation process is not performed as for untrusted classes. In addition to the bootstrap class loader, the JVM has an extension class loader responsible for loading classes from standard extension APIs, and a system class loader that loads classes from a general class path as well as your application classes.

Since there is more than one class loader, they are represented in a tree whose root is the bootstrap class loader. Each class loader has a reference to its parent class loader. When a class loader is asked to load a class, it consults its parent class loader before attempting to load the item itself. The parent in turn consults its parent, and so on. So it is only after all the ancestor class loaders cannot find the class that the current class loader gets involved. In other words, a delegation model is used.

The java.lang.ClassLoader Class

The java.lang.ClassLoader is an abstract class that can be subclassed by applications that need to extend the manner in which the JVM dynamically loads classes. Constructors injava.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. In other words, the ClassLoader class uses a delegation model to search for classes and resources. Therefore, each instance of ClassLoader has an associated parent class loader, so that when requested to find a class or resources, the task is delegated to its parent class loader before attempting to find the class or resource itself. The loadClass() method of the ClassLoader performs the following 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 methodfindClass() to find and load the class. The finalClass() method searches for the class in the current class loader if the class wasn't found by the parent class loader.


There's more in the original article, which also shows you how to implement your own network class loaders, which answers your question as to why (and how). See also the API docs.

 

The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.

 

The question is "Why should one bother this ClassLoader class exists" ?

Well, mostly so you can fix things if they go wrong :-).

It's true, as long as you just write an application, compile it to a JAR and maybe include a few extra library JARs, you don't need to know about class loaders, it will just work.

Still, it is helpful to know a bit about class loaders and class loading to better understand what goes on behind the scenes. As an example, "static initializers" will run when a class is loaded, so to understand when they will run, you need to know how the class loader decides when to load them.

also.. how do you use it in practice ?

For simple cases, you don't need them. However, if you need to load code dynamically at runtime with explicit control where it comes from (e.g. loading over a network, loading plugins not available at compile time, etc.), you may need to do more. Then you can e.g. write your own class loader. See the other answers for links.

let's see some implemented classloader for your own.

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;



public class FileClassLoader extends ClassLoader {

  public Class findClass(String name) {

    byte[] data = loadClassData(name);

    return defineClass(name, data, 0, data.length);

  }

  

  private byte[] loadClassData(String name) {

    FileInputStream fis = null;

    byte[] data = null;

    try {

      fis = new FileInputStream(new File("D:\\project\\test\\" + name + ".class"));

      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      int ch = 0;

      while ((ch = fis.read()) != -1) {

        baos.write(ch);

      }

      data = baos.toByteArray();

    } catch (IOException e) {

      e.printStackTrace();

    }

    return data;

  }

}





public class MyApp {

  public static void main(String[] args) throws Exception {

    FileClassLoader loader = new FileClassLoader();

    Class objClass = loader.findClass("MyApp");

    Object obj = objClass.newInstance();

    System.out.println(objClass.getName());

    System.out.println(objClass.getClassLoader());

    System.out.println(obj);

  }

}

the result is 

MyApp

FileClassLoader@757aef

MyApp@9cab16

 

 

See also : ClassLoader

               JLS 12.4

 

 

 

你可能感兴趣的:(ClassLoader)