Summary
Java class Loader is the core of Java dynamics, this article will brief you on Java class loader, and related parent delegation model, namespace, runtime package concepts, while discussing some of the problems that are easy to confuse in learning.
function and classification of class loaders
As the name suggests, class loaders are used to load classes (class) into the JVM. The JVM specification defines two types of class Loaders: The Start class loader (bootstrap) and the user custom loader (user-defined class loader).
Bootstrap is the JVM's own class loader, used to load core class libraries, such as java.lang.*. If the Java.lang.Object is loaded by bootstrap.
Java provides an abstract class ClassLoader that all user-defined class loaders instantiate from ClassLoader subclasses. System class loader is a special user-defined class loader, provided by the JVM's implementation, that loads the user class by default when the programmer does not specifically specify the loader. The System class loader can be obtained by means of the Classloader.getsystemclassloader () method.
Example 1, test the ClassLoader of the JVM you are using
/*loadersample1.java*/public class LoaderSample1 {public static void main (string[] args) {Class C; ClassLoader cl;
CL = Classloader.getsystemclassloader (); SYSTEM.OUT.PRINTLN (CL);
while (cl != null) { cl = cl.getparent (); system.out.println (CL); } try { c = class.forname ("Java.lang.Object"); cl = c.getclassloader (); system.out.println ("Java.lang.Object ' S loader is " + CL); c = Class.forName ("LoaderSample1"); cl = c.getclassloader (); system.out.println ("LoaderSample1 ' s loader is " + CL); } catch ( Exception e) { E.printstacktrace (); } }}
Running results on my machine (Sun Java 1.5)
C:\java>java LoaderSample1
Sun.misc.launcher$appclassloader@82ba41
Sun.misc.launcher$extclassloader@923e30
Null
Java.lang.Object ' s loader is null
LoaderSample1 ' s loader is Sun.misc.launcher$appclassloader@82ba41
The first line indicates that the System class loader is instantiated from the class Sun.misc.launcher$appclassloader
The second line indicates that the parent of the System class loader is instantiated from the class Sun.misc.launcher$extclassloader
The third line indicates that the parent of the System class loader parent is bootstrap
The four lines indicate that the core class Java.lang.Object is loaded by bootstrap
Line five indicates that the user class LoaderSample1 is loaded by the System class loader
Parent delegation Model
Starting with version 1.2, Java introduced a parental delegation model to better secure the Java platform. In this model, when a loader is requested to load a class, it first entrusts its own parent to load, if the parent can load, then return the class object corresponding to it, if the parent can not be loaded, the parent's requester to load.
As shown in Figure 1, Loader2 's parent is Loader1,loader1 's parent for System class loader. Assuming Loader2 is required to load class MyClass, under the parent delegation model, LOADER2 first requests Loader1 to load, Loader1 then requests the System class loader to load MyClass. If the system loader succeeds in loading, the reference of the class object corresponding to the MyClass is returned to the Loader1,loader1 and the reference is returned to Loader2, thereby successfully loading the classes MyClass into the virtual machine. If the System class loader cannot load the Myclass,loader1 will attempt to load the MyClass, if Loader1 also cannot mount successfully, LOADER2 will attempt to load. If all the parent and loader2 themselves cannot be loaded, the mount fails.
If there is a successful mount, the actual loaded class loader is called the definition class loader, and all the loaders that can successfully return the class object (including the definition class loader) are called the initial class loaders. As shown in Figure 1, assuming that the loader1 actually loads the MyClass, the loader1 is the definition class loader for MyClass, Loader2 and Loader1 as the initial class loader for MyClass.
Current 1/2 page
12 Next read the full text