Class loader name space summary and analysis point extension class loader
The parent class loader delegation model benefits:
- Ensures type safety of Java core libraries: All Java applications will be cited at least java.lang.Object class, which means that at runtime, java.lang.Object This class will be loaded into the Java virtual machine; if the load Java application process by its own class loader completed, it is likely that there are several versions will java.lang.Object class in the JVM, but also the incompatibility between these classes, another invisible ( formal namespace play a role).
Parents commissioned by means of mechanisms, work load classes in Java core libraries are to unify done by the root class loader, thus ensuring the use of the Java application are the same version of Java core libraries, among them They are mutually compatible.
- Java core libraries can be sure that the class is offered can not be replaced by a custom class.
- Different class loaders can create additional space for the name of the same name (binary name) class. And the class with the same name can exist in the Java virtual machine, simply use a different class loader loads they can be. Between different class loader loaded the class is not compatible, which is equivalent to create a Java class space one after another isolated from each other inside the Java virtual machine, such loads in many frameworks have been practical application.
Extension class loader:
public class MyTest22 {
static {
System.out.println("MyTest22 initializer");
}
public static void main(String[] args) throws Exception {
System.out.println(MyTest22.class.getClassLoader());
System.out.println(MyTest21.class.getClassLoader());
}
}
运行结果:
MyTest22 initializer
sun.misc.Launcher$AppClassLoader@18b4aac2
sun.misc.Launcher$AppClassLoader@18b4aac2
It can be seen that: The final results of the operation, MyTest22, MyTest21 by the system class loader is loaded.
Adjustment:
The final result:
a different point:
At runtime, a Java class is (binary name, the name of the binary) by a fully qualified name of the class and the common decision for loading the class definition of class loader (defining loader).
If the same name (i.e., the same fully qualified name) class is loaded by two different loader, then the category is different, even if the bytecode .class file exactly the same, and also to load from the same location It is so.
Implementation, system attributes in Oracle's Hotspot sun.boot.class.path If you modify wrong, will go wrong run, it suggests the following error message:
...out\production\IdeaProject\Jvm>java -Dsun.boot.class.path=./ Jvm.MyTest23 Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object