We all know that Java class loaders have three levels: Bootstrap, extension, and system. The three loaders are parent-child, with the bootstrap class loader at the top and the system loader at the bottom of the structure. At the same time, they all adopt the upward transfer mechanism, that is, there is a class to be loaded. First, ask your parent loader. If the parent loader cannot be loaded, load it by yourself.
We wrote a helloworld class. During loading, the binary bytecode is sent to the system class loader, but the system class will ask its parent loader (extension class). Can you load it? The extension class loader will also ask your parent loader (pilot class) If you can load it?
In layman's terms, the three loaders of the guided extension system are just like grandfathers and grandsons. They are very filial to loading classes. When loading classes, they should let elders load them first, elders cannot load themselves.
What are the benefits of doing so?
If you write a full name for Java. lang. object Class, which contains some dangerous code. During loading, the system class loader will first try to load .. finally, the bootstrap class is loaded. It will directly go to c: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ RT according to the path. find Java. lang. object. All in all, safe!
public class Property{ public static void main(String[] args) { System.out.println("boot "+System.getProperty("sun.boot.class.path")); System.out.println("ext "+System.getProperty("java.ext.dirs")); System.out.println("system "+System.getProperty("java.class.path")); try { System.out.println(Class.forName("Property").getClassLoader()+"PPP"); } catch (Exception e) { e.printStackTrace(); } }}
Running result
Boot c: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ resources. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ RT. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ sunrsasign. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ JSSE. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ JCE. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ charsets. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ jfr. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Classes
EXT c: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ ext; C: \ WINDOWS \ sun \ Java \ Lib \ ext
System .; c: \ Program Files \ Java \ jdk1.7.0 _ 15 \ Lib \ DT. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ Lib \ tools. jar; C: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ RT. jar; E: \ apache-tomcat-7.0.47_8700 \ Lib \ servlet-api.jar;
[Email protected]
The above example shows the loading range of the Three-Level loader;
Now I have an idea: if we put the class file of the property class under the directory of the extension class loader, will it be loaded by the extension class loader?
OK. I put the class file of the property class under the directory of the extension class loader, and the result will not change;
It's strange. Isn't the extension Loader ready to load files in the directory c: \ Program Files \ Java \ jdk1.7.0 _ 15 \ JRE \ Lib \ Ext?
Or is it compressed into a jar file?
Do you want to change the compression method?
For example
Now we have at least a few questions.
1. Under the loading directory of the extension class loader, only the files in jar format are recognized.
2. jar must also be in zip compression format!
It took me two hours to worry about such a small problem.
Loading of extended class loaders