Hot Spot recommendation: deep understanding of Java class loaders
First, let's review the steps for loading a java class on the java VM: After the java file is compiled by the compiler, it becomes a bytecode file. class file), the class Loader (ClassLoader) read. class file and convert it to java. lang. an instance of the Class, and finally an object of the Class is created through the newInstance method. ClassLoader is used to locate the corresponding bytecode based on a Class name and define the corresponding Class based on these bytecode. This Class is an instance of java. lang. Class.
Organizational structure of the Class Loader
Java has three initialization class loaders. When the java Virtual Machine starts, they start in the following order: Bootstrap classloader-> extension classloader-> system classloader. Relationship: bootstrap classloader is the parent of extension classloader, and extension classloader is the parent of system classloader.
Bootstrap classloader
It is the most primitive class loader, not written by java code, but written by native code. Java once compiled and runs on all platforms because it writes the same underlying code for different languages on different platforms. It is responsible for loading the java core library. You can run the following code to see where your local java core library is:
- URL[] urls=sun.misc.Launcher.getBootstrapClassPath().getURLs();
- for (int i = 0; i < urls.length; i++) {
- System.out.println(urls[i].toExternalForm());
- }
My running result:
- file:/home/eric/jdk1.6.0_35/jre/lib/resources.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/rt.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/sunrsasign.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/jsse.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/jce.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/charsets.jar
- file:/home/eric/jdk1.6.0_35/jre/lib/modules/jdk.boot.jar
- file:/home/eric/jdk1.6.0_35/jre/classes
Extension classloader
It is used to load the JAR class package specified by JAVA_HOME/JRE/lib/ext or java. ext. dirs system attribute. Note that because it is loaded by bootstrap classloader, when you run:
ClassLoader extensionClassloader = ClassLoader. getSystemClassLoader (). getParent ();
System. out. println ("the parent of extension classloader:" + extensionClassloader. getParent ());
The output is: the parent of extension classloader: null
System classloader
It is used to load the jar package under the classpath directory. the java class we write is usually loaded by it unless you have made your own class loader.
Overall entrusting Mechanism
When a classloader loads a class, it uses the full-responsibility delegation mechanism. It can be understood in two parts: Full-responsibility and delegation.
Full responsibility mechanism: if Class A calls Class B, all jar packages introduced by class B and class B are uniformly loaded by the class loaders of Class.
Delegation mechanism: when the class loader loads Class A, it first loads the parent loader. If the parent loader fails to load, it will find the parent loader again, the bootstrap classloader cannot be found until it finds the relevant path for loading. The following is the source code of ClassLoader:
- Protected synchronized Class <?> LoadClass (String name, boolean resolve)
- Throws ClassNotFoundException
- {
- // First, check if the class has already been loaded
- Class c = findLoadedClass (name );
- If (c = null ){
- Try {
- If (parent! = Null ){
- // Load from the parent Loader
- C = parent. loadClass (name, false );
- } Else {
- // Load from bootstrap loader
- 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 );
- }
- Return c;
- }
For example, the classloader loads Class:
1. Check whether it has been loaded and search in the cache. If yes, skip 7; otherwise, go to the next step.
2. Determine whether the current loader has a parent loader. If no parent loader exists, the current loader is ext classloader and the next step is skipped.
3. Request the parent loader to load the class. If the loading is successful, skip 7. If the loading fails, that is, the parent loader cannot find the class. Skip 2.
4. Request the jvm bootstrap classloader to load. If the load is successful, skip 7. If the load fails, skip 5.
5. The current loader is loaded by itself. If it succeeds, it will jump to 7. Otherwise, it will jump to 6.
6. Throw ClassNotFoundException.
7. Return Class
Compile your own class loader
The process of loading classes in Java is essentially to call the loadClass () method. In loadClass, The findLoadedClass () method is called to check whether the class has been loaded, if no, the loadClass () of the parent loader is called. If the parent loader cannot load the class, the findClass () is called to find the class.
Therefore, we need to create a new MyClassLoader to inherit java. lang. ClassLoader and override the findClass () method. It is mainly to redesign the scheme of searching for bytecode files, and then call definedClass to return the results.
I wrote a demo and used my class loader to load the specified java file with hot deployment effect. For details, see the following url.
Demo address: http://git.oschina.net/ericquan8/hot-deploy