Class Loader:
The class loader (ClassLoader) is used to load the class bytecode into a Java virtual machine. In general, Java virtual machines use Java classes in the following way: Java source files are converted to Java bytecode files (. class files) after Javac. The ClassLoader is responsible for reading the Java byte code and converting it into an instance of the Java.lang.Class class. Each of these instances is used to represent a Java class. The actual situation can be more complex, such as the Java byte code may be generated dynamically through the tool, or it may be downloaded over the network.
Class with class loader:
Although the ClassLoader is only used to implement the load action of a class, its role in Java programs is far from being limited to the class loading phase. for any class, it is necessary to establish its uniqueness in Java virtual with the class loader that loads it and the class itself . To say, compare two classes is "equal", only if two classes are the same class loader under the premise of meaning , otherwise, even if the two classes from the same class file, as long as the loading of its classloader is different, then the two classes must be unequal . The "equality" referred to here includes the equal method, the IsAssignableFrom (), the Isinstance () method, and the result returned by the INSTANCEOF keyword representing the class object.
Class Loader Categories:
Mainly divided into bootstrap ClassLoader, Extension ClassLoader, application ClassLoader and user Defined ClassLoader.
Start the class loader (Bootstrap ClassLoader):
This classloader is implemented using the C + + language and is not a subclass of ClassLoader. It is primarily responsible for loading all the class files stored in the Java_home/jre/lib/rt.jar, or Rt.jar named in the path specified by the-xbootclasspath parameter .
Extension class loader (Extension ClassLoader):
This loader is implemented by Sun.misc.launcher$extclassloader and is responsible for loading all class libraries in the Java_home/lib/ext directory, or in the path specified by the Java.ext.dirs system variable.
Application class loader (application ClassLoader):
This loader is implemented by Sun.misc.launcher$appclassloader, which is responsible for loading classpath corresponding jars and directories . In general, this is the default class loader in the program.
Custom class loader (User Defined ClassLoader):
The developer inherits the ClassLoader class loader, which is self-implemented by the abstract class, and can be used on a self-developed classloader that is not loaded classpath (for example, jar or binary bytecode downloaded from the network), and can do little tricks before loading the class file. such as: encryption and so on.
Parent-Commissioned Model:
This hierarchical relationship between the class loaders shown in is called the class loader's parent-delegate model . The parental delegation model requires that the rest of the ClassLoader should have its own parent ClassLoader, in addition to the top-level startup class loader . The parent-child relationship between ClassLoader is generally not implemented in an inherited relationship , but instead uses the code of the parent loader with the combined relationship .
Public Abstract classClassLoader {Private Static native voidregisternatives (); Static{registernatives (); } //The parent class loader for delegation PrivateClassLoader Parent; //Hashtable that maps packages to certs PrivateHashtable Package2certs =NewHashtable (11); }
Parent-commissioned work process: If a class loader receives a class load request, it does not load the class on its own, but instead delegates the request to the parent ClassLoader, which is the case for each level of the ClassLoader. So all load requests should eventually be routed to the top-level startup class loader, and the child loader will attempt to load itself only when the parent ClassLoader has fed back its inability to complete the load request (which is not in the scope of its management) .
The obvious benefit of using a parent-delegate model to organize relationships between ClassLoader is that the Java class has a hierarchical relationship with precedence as its classloader. For example, Java.lang.Object stored in the Rt.jar, regardless of the class loader to load this class, and ultimately commissioned to the startup class loader to load, so the object class in the program's various class loader environment is the same class, conversely, if there is no parent delegate model, by the various classloader to complete, if The user writes a class named Java.lang.Object, and puts it in the classpath, there may be several different object classes in the application, and the most basic security behavior in the Java type system cannot be guaranteed.
Class Loader SPI:
The Java.lang.ClassLoader class provides several key methods:
LoadClass: This method is responsible for loading the class of the specified name, first from the loaded class, if not found, loaded from parent Classloader[extclassloader], if not loaded, from bootstrap ClassLoader in the attempt to load (Findbootstrapclassornull method), if the load fails, throws an exception classnotfoundexception, The Findclass method is called to load itself . You can override this method if you want to change the load order of the classes, or you can override the Findclass method to do special processing, such as decryption, fixed path seek, and so on, if the loading order is the same. The classnotfoundexception exception is thrown when the class object is still not acquired by the process of searching through the whole classes.
If the class requires resolve, the link is called Resolveclass.
protected synchronizedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {//First , check if the class has already been loadedClass C =Findloadedclass (name); if(c = =NULL) { Try { if(Parent! =NULL) {C= Parent.loadclass (Name,false); } Else{C=findbootstrapclassornull (name); } } Catch(ClassNotFoundException e) {//ClassNotFoundException thrown if class not found//From the Non-null parent class loader } if(c = =NULL) { //If still not found, then invoke Findclass in order//To find the class. c =Findclass (name); } } if(Resolve) {resolveclass (c); } returnC; }
Findloadedclass This method is responsible for finding the loaded class from the cache of the current ClassLoader instance object, called the native method.
protected Final Class<?> Findloadedclass (String name) { (! CheckName (name)) returnnull; Urn FindLoadedClass0 (name); } Private native Final
Findsystemclass This method is to look for the class from the Sun.misc.launcher$appclassloader, if it is not found, continues to look from the Bootstrapclassloader, and returns null if it is still not found
protected FinalClass<?>Findsystemclass (String name)throwsclassnotfoundexception {ClassLoader system=Getsystemclassloader (); if(System = =NULL) { if(!checkname (name))Throw Newclassnotfoundexception (name); Class CLS=Findbootstrapclass (name); if(CLS = =NULL) { Throw Newclassnotfoundexception (name); } returnCLS; } returnSystem.loadclass (name); }
DefineClass This method is responsible for converting the binary byte stream to a class object, which is very important for a custom class loader . If the format of the binary bytecode does not conform to the JVM class file format specification, the Classformaterror exception is thrown, and if the generated class name and binary bytecode are different, the noclassdeffounderror is thrown, if the loaded class is protected, The SecurityException exception is thrown with a different signature, or if the class name starts with Java.
protected FinalClass<?> defineclass (String name,byte[] B,intOffintLen, Protectiondomain protectiondomain)throwsClassformaterror {returnDefineclasscond (name, B, off, Len, Protectiondomain,true); } //Private method w/an extra argument for skipping class verification Private FinalClass<?>Defineclasscond (String name,byte[] B,intOffintLen, Protectiondomain Protectiondomain, BooleanVerify)throwsClassformaterror {protectiondomain=Predefineclass (name, Protectiondomain); Class C=NULL; String Source=defineclasssourcelocation (Protectiondomain); Try{C=DefineClass1 (name, B, off, Len, Protectiondomain, source, verify); } Catch(Classformaterror CfE) {C=Definetransformedclass (name, B, off, Len, Protectiondomain, CFE, source, Veri FY); } postdefineclass (c, Protectiondomain); returnC; }
Resolveclass This method is responsible for completing the link to the class object, and if the link is received, it is returned directly.
Common exceptions:
ClassNotFoundException This is the most common exception, the reason for this exception is that the class file was not found when the class was loaded in the current ClassLoader,
Noclassdeffounderror This exception is because the other class referenced in the loaded class does not exist, for example, to load a, and a refers to a b,b that does not exist or the current ClassLoader cannot load B, this exception is thrown.
Linkageerror This exception is more likely to occur in the case of custom classloader, mainly because this class has already been loaded in ClassLoader and repeated loading causes the exception.
This text link: http://blog.csdn.net/java2000_wl/article/details/8222876 reprint please specify the source!
Java class Loader