In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the role of classloader in JVM". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of classloader in JVM".
Method
Public Class loadClass (String name) throws ClassNotFoundException uses the class name to play the Class instance of the class
Protected final Class defineClass (byte [] b class int off,int len) represents the position and length of the actual class information in the byte array according to the given bytecode stream bforce off and len parameters, where the byte array b is obtained from the outside by classloader.
Protected Class findClass (String name) throws ClassNotFoundException view a class
Protected final Class findLoadedClass (String name)
classification
BootStrap ClassLoader
Extension ClassLoader
The AppClassLoader startup class loader is responsible for loading the core classes of the system (rt.jar 's java class), the extension class loader loads the classes in% JAVA_HOME/lib/ext/*.jar, the application class loader is used to load user classes (classpath), and the custom class loader loads classes with some special paths (custom classloader).
Parental entrustment
Currently, classloader first queries whether the class has been loaded from the class it has loaded, and returns the class that has been loaded directly if it has already been loaded.
When the loaded class is not found in the current classloader cache, the parent class loader is entrusted to load it. The parent class loader uses the same strategy, first check its own cache, and then delegate the parent class to load until bootstrap classloader.
When all the parent class loaders are not loaded, it will be loaded by the current class loader, put it in its own cache, and return directly on the next request.
Keep repeating in a cycle
Action
The basic classes of each class loader are unified.
Jar-cvf test.jar HelloLoader.class packages class into jar
Extension ClassLoader
Example: drop your own jar package in the ext path and load it
Package com.mousycoder.server;public class HelloWorld {public static void main (String [] args) {System.out.println ("HelloWorld!");}}
Idea generates helloworld.jar through structs- > artifacts- > jar and then build- > build artifacts- > build to / Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext
Package com.mousycoder.mycode.thinking_in_jvm;import java.lang.reflect.Method;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-06 10:35 * / public class ExtClassLoader {public static void main (String [] args) throws ClassNotFoundException {System.out.println (System.getProperty ("java.ext.dirs")); Class helloClass = Class.forName ("com.mousycoder.server.HelloWorld") System.out.println (helloClass.getClassLoader ());}}
Output
/ Users/mousycoder/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/javasun.misc.Launcher$ExtClassLoader@610455d6
You can see that ExtClassLoader loaded the java.ext.dirs directory
Custom class loader package com.mousycoder.mycode.thinking_in_jvm;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-06 11:13 * / public class MyClassLoader extends ClassLoader {private final static Path DEFAULT_CLASS_PATH = Paths.get ("," / Users/mousycoder/My ") Private final Path classDir; public MyClassLoader () {super (); this.classDir = DEFAULT_CLASS_PATH;} public MyClassLoader (String classDir) {super (); this.classDir = Paths.get (classDir);} public MyClassLoader (String classDir, ClassLoader parent) {super (parent); this.classDir = Paths.get (classDir) } @ Override protected Class findClass (String name) throws ClassNotFoundException {try {byte [] classBytes = this.readClassBytes (name); if (null = = classBytes | | 0 = = classBytes.length) {throw new ClassNotFoundException ("can not load the class" + name);} return this.defineClass (name,classBytes,0,classBytes.length) } catch (IOException e) {e.printStackTrace ();} return null;} private byte [] readClassBytes (String name) throws ClassNotFoundException, IOException {String classPath = name.replace (".", "/"); Path classFullPath = classDir.resolve ("HelloWorld1.class") If (! classFullPath.toFile (). Exists ()) {throw new ClassNotFoundException ("The class" + name + "mpt found");} try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {Files.copy (classFullPath,baos); return baos.toByteArray ();} catch (IOException e) {throw new ClassNotFoundException ("load the class" + name + "occur error", e) } @ Override public String toString () {return "MyClassLoader";}} package com.mousycoder.mycode.thinking_in_jvm;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-06 11:34 * / public class MyClassLoaderTest {public static void main (String [] args) throws ClassNotFoundException {MyClassLoader classLoader = new MyClassLoader () Class class1 = classLoader.loadClass ("com.mousycoder.mycode.thinking_in_jvm.HelloWorld1"); System.out.println (class1.getClassLoader ());}} package com.mousycoder.mycode.thinking_in_jvm;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-06 11:46 * / public class HelloWorld1 {public static void main (String [] args) {System.out.println ("Hello world1");}}
Change helloword1 to class and put it in the / Users/mousycoder/My directory to output My ClassLoader on behalf of the custom class loader to load the class
Context class loader
Action
Break the parent delegation mechanism, so that the upper parent class loader can use the subclass loader to load objects, such as the interface class in Spi in the system loader, but the implementation class in the application loader
Tomcat class loader purpose
Ensure that the class libraries of each application are isolated independently (even if there are different versions of the same qualified name)
Guarantee the sharing of the same class library and the same version of the class library
Ensure that the container's own class libraries and programs are independent
Loading sequence
Bootstrap Boot Class Loader
System system class loader
Application class loader WEB-INF/classes
Application class loader WEB-INF/lib
Common class loader CATALINA/lib
Thank you for reading, the above is the content of "what is the role of classloader in JVM". After the study of this article, I believe you have a deeper understanding of what the role of classloader in JVM is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.