ClassLoader翻译过来就是类加载器,普通的java开发者其实用到的不多,但对于某些框架开发者来说却非常常见。理解ClassLoader的加载机制,也有利于我们编写出更高效的代码。ClassLoader的具体作用就是将class文件加载到jvm虚拟机中去,程序就可以正确运行了。但是,jvm启动的时候,并不会一次性加载所有的class文件,而是根据需要去动态加载。想想也是的,一次性加载那么多jar包那么多class,那内存不崩溃。本文的目的也是学习ClassLoader这种加载机制。
Java Class字节码加载路径认识
一 模拟JVM加载过程
/**
* @description ClassPathClassLoader
* @date 2020/12/22 8:21 下午
*/
public class ClassPathClassLoader {
public static void main(String[] args)
{
System.out.println("BootstrapClassLoader 的加载路径: ");
URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
for(URL url : urls) {
System.out.println(url);
}
System.out.println("----------------------------");
//取得扩展类加载器
URLClassLoader extClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader().getParent();
System.out.println(extClassLoader);
System.out.println("扩展类加载器 的加载路径: ");
urls = extClassLoader.getURLs();
for(URL url : urls)
System.out.println(url);
System.out.println("----------------------------");
//取得应用(系统)类加载器
URLClassLoader appClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
System.out.println(appClassLoader);
System.out.println("应用(系统)类加载器 的加载路径: ");
urls = appClassLoader.getURLs();
for(URL url : urls) {
System.out.println(url);
}
System.out.println("----------------------------");
}
}
二 虚拟机加载路径分析
/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49713:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/zhangguilong/development/codes/droplet/droplet-core/target/classes:/Users/zhangguilong/development/codes/droplet/droplet-common/target/classes:/development/runtime-environment/repository/com/weiease/rdc/rdc-common/1.0.9/rdc-common-1.0.9.jar:/development/runtime-environment/repository/org/springframework/spring-context/5.2.0.RELEASE/spring-context-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/springframework/spring-aop/5.2.0.RELEASE/spring-aop-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/springframework/spring-beans/5.2.0.RELEASE/spring-beans-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/springframework/spring-expression/5.2.0.RELEASE/spring-expression-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/slf4j/slf4j-api/1.7.9/slf4j-api-1.7.9.jar:/development/runtime-environment/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/development/runtime-environment/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/development/runtime-environment/repository/org/springframework/data/spring-data-commons/2.3.4.RELEASE/spring-data-commons-2.3.4.RELEASE.jar:/development/runtime-environment/repository/org/projectlombok/lombok/1.18.10/lombok-1.18.10.jar:/development/runtime-environment/repository/org/springframework/spring-core/5.2.0.RELEASE/spring-core-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/springframework/spring-jcl/5.2.0.RELEASE/spring-jcl-5.2.0.RELEASE.jar:/development/runtime-environment/repository/org/reflections/reflections/0.9.10/reflections-0.9.10.jar:/development/runtime-environment/repository/com/google/guava/guava/22.0/guava-22.0.jar:/development/runtime-environment/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/development/runtime-environment/repository/com/google/errorprone/error_prone_annotations/2.0.18/error_prone_annotations-2.0.18.jar:/development/runtime-environment/repository/com/google/j2objc/j2objc-annotations/1.1/j2objc-annotations-1.1.jar:/development/runtime-environment/repository/org/codehaus/mojo/animal-sniffer-annotations/1.14/animal-sniffer-annotations-1.14.jar:/development/runtime-environment/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/development/runtime-environment/repository/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1.jar:/development/runtime-environment/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-starter/2.7.4.1/dubbo-spring-boot-starter-2.7.4.1.jar:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-autoconfigure/2.7.4.1/dubbo-spring-boot-autoconfigure-2.7.4.1.jar:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-autoconfigure-compatible/2.7.4.1/dubbo-spring-boot-autoconfigure-compatible-2.7.4.1.jar:/development/runtime-environment/repository/org/apache/dubbo/dubbo/2.7.4.1/dubbo-2.7.4.1.jar:/development/runtime-environment/repository/io/netty/netty-all/4.1.25.Final/netty-all-4.1.25.Final.jar:/development/runtime-environment/repository/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar:/development/runtime-environment/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-discovery/2.2.3.RELEASE/spring-cloud-starter-alibaba-nacos-discovery-2.2.3.RELEASE.jar:/development/runtime-environment/repository/com/alibaba/nacos/nacos-client/1.3.3/nacos-client-1.3.3.jar:/development/runtime-environment/repository/com/alibaba/nacos/nacos-common/1.3.3/nacos-common-1.3.3.jar:/development/runtime-environment/repository/commons-io/commons-io/2.2/commons-io-2.2.jar:/development/runtime-environment/repository/org/apache/httpcomponents/httpasyncclient/4.1.3/httpasyncclient-4.1.3.jar:/development/runtime-environment/repository/org/apache/httpcomponents/httpcore-nio/4.4.6/httpcore-nio-4.4.6.jar:/development/runtime-environment/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/development/runtime-environment/repository/com/alibaba/nacos/nacos-api/1.3.3/nacos-api-1.3.3.jar:/development/runtime-environment/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-core/2.10.4/jackson-core-2.10.4.jar:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-databind/2.10.4/jackson-databind-2.10.4.jar:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.4/jackson-annotations-2.10.4.jar:/development/runtime-environment/repository/io/prometheus/simpleclient/0.5.0/simpleclient-0.5.0.jar:/development/runtime-environment/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/development/runtime-environment/repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-commons/2.2.5.RELEASE/spring-cloud-commons-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/security/spring-security-crypto/5.3.3.RELEASE/spring-security-crypto-5.3.3.RELEASE.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-context/2.2.5.RELEASE/spring-cloud-context-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter-netflix-ribbon/2.2.5.RELEASE/spring-cloud-starter-netflix-ribbon-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter/2.2.5.RELEASE/spring-cloud-starter-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/boot/spring-boot-starter/2.3.2.RELEASE/spring-boot-starter-2.3.2.RELEASE.jar:/development/runtime-environment/repository/org/springframework/boot/spring-boot/2.3.2.RELEASE/spring-boot-2.3.2.RELEASE.jar:/development/runtime-environment/repository/org/springframework/boot/spring-boot-autoconfigure/2.3.2.RELEASE/spring-boot-autoconfigure-2.3.2.RELEASE.jar:/development/runtime-environment/repository/org/springframework/boot/spring-boot-starter-logging/2.3.2.RELEASE/spring-boot-starter-logging-2.3.2.RELEASE.jar:/development/runtime-environment/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/development/runtime-environment/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/development/runtime-environment/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/development/runtime-environment/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/development/runtime-environment/repository/org/springframework/security/spring-security-rsa/1.0.9.RELEASE/spring-security-rsa-1.0.9.RELEASE.jar:/development/runtime-environment/repository/org/bouncycastle/bcpkix-jdk15on/1.64/bcpkix-jdk15on-1.64.jar:/development/runtime-environment/repository/org/bouncycastle/bcprov-jdk15on/1.64/bcprov-jdk15on-1.64.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-netflix-ribbon/2.2.5.RELEASE/spring-cloud-netflix-ribbon-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-netflix-archaius/2.2.5.RELEASE/spring-cloud-netflix-archaius-2.2.5.RELEASE.jar:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter-netflix-archaius/2.2.5.RELEASE/spring-cloud-starter-netflix-archaius-2.2.5.RELEASE.jar:/development/runtime-environment/repository/com/netflix/archaius/archaius-core/0.7.6/archaius-core-0.7.6.jar:/development/runtime-environment/repository/commons-configuration/commons-configuration/1.8/commons-configuration-1.8.jar:/development/runtime-environment/repository/com/netflix/ribbon/ribbon/2.3.0/ribbon-2.3.0.jar:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-transport/2.3.0/ribbon-transport-2.3.0.jar:/development/runtime-environment/repository/io/reactivex/rxnetty-contexts/0.4.9/rxnetty-contexts-0.4.9.jar:/development/runtime-environment/repository/io/reactivex/rxnetty-servo/0.4.9/rxnetty-servo-0.4.9.jar:/development/runtime-environment/repository/com/netflix/hystrix/hystrix-core/1.4.3/hystrix-core-1.4.3.jar:/development/runtime-environment/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/development/runtime-environment/repository/io/reactivex/rxnetty/0.4.9/rxnetty-0.4.9.jar:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-core/2.3.0/ribbon-core-2.3.0.jar:/development/runtime-environment/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-httpclient/2.3.0/ribbon-httpclient-2.3.0.jar:/development/runtime-environment/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/development/runtime-environment/repository/com/sun/jersey/jersey-client/1.19.1/jersey-client-1.19.1.jar:/development/runtime-environment/repository/com/sun/jersey/jersey-core/1.19.1/jersey-core-1.19.1.jar:/development/runtime-environment/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar:/development/runtime-environment/repository/com/sun/jersey/contribs/jersey-apache-client4/1.19.1/jersey-apache-client4-1.19.1.jar:/development/runtime-environment/repository/com/netflix/servo/servo-core/0.10.1/servo-core-0.10.1.jar:/development/runtime-environment/repository/com/netflix/servo/servo-internal/0.10.1/servo-internal-0.10.1.jar:/development/runtime-environment/repository/com/netflix/netflix-commons/netflix-commons-util/0.1.1/netflix-commons-util-0.1.1.jar:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-loadbalancer/2.3.0/ribbon-loadbalancer-2.3.0.jar:/development/runtime-environment/repository/com/netflix/netflix-commons/netflix-statistics/0.1.1/netflix-statistics-0.1.1.jar:/development/runtime-environment/repository/io/reactivex/rxjava/1.3.8/rxjava-1.3.8.jar:/development/runtime-environment/repository/org/aspectj/aspectjweaver/1.8.6/aspectjweaver-1.8.6.jar:/development/runtime-environment/repository/com/alibaba/fastjson/1.2.48/fastjson-1.2.48.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-connector-basic/1.0.2.v20150114/aether-connector-basic-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-api/1.0.2.v20150114/aether-api-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-spi/1.0.2.v20150114/aether-spi-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-util/1.0.2.v20150114/aether-util-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-file/1.0.2.v20150114/aether-transport-file-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-http/1.0.2.v20150114/aether-transport-http-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/apache/httpcomponents/httpclient/4.5/httpclient-4.5.jar:/development/runtime-environment/repository/org/apache/httpcomponents/httpcore/4.4.1/httpcore-4.4.1.jar:/development/runtime-environment/repository/org/slf4j/jcl-over-slf4j/1.6.2/jcl-over-slf4j-1.6.2.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-wagon/1.0.2.v20150114/aether-transport-wagon-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-provider-api/1.0/wagon-provider-api-1.0.jar:/development/runtime-environment/repository/org/apache/maven/maven-aether-provider/3.3.9/maven-aether-provider-3.3.9.jar:/development/runtime-environment/repository/org/apache/maven/maven-model/3.3.9/maven-model-3.3.9.jar:/development/runtime-environment/repository/org/apache/maven/maven-model-builder/3.3.9/maven-model-builder-3.3.9.jar:/development/runtime-environment/repository/org/codehaus/plexus/plexus-interpolation/1.21/plexus-interpolation-1.21.jar:/development/runtime-environment/repository/org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9.jar:/development/runtime-environment/repository/org/apache/maven/maven-builder-support/3.3.9/maven-builder-support-3.3.9.jar:/development/runtime-environment/repository/org/apache/maven/maven-repository-metadata/3.3.9/maven-repository-metadata-3.3.9.jar:/development/runtime-environment/repository/org/eclipse/aether/aether-impl/1.0.2.v20150114/aether-impl-1.0.2.v20150114.jar:/development/runtime-environment/repository/org/codehaus/plexus/plexus-component-annotations/1.6/plexus-component-annotations-1.6.jar:/development/runtime-environment/repository/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-ssh/1.0/wagon-ssh-1.0.jar:/development/runtime-environment/repository/com/jcraft/jsch/0.1.44-1/jsch-0.1.44-1.jar:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-ssh-common/1.0/wagon-ssh-common-1.0.jar:/development/runtime-environment/repository/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-6/plexus-interactivity-api-1.0-alpha-6.jar com.weiease.droplet.plugin.ClassPath_of_Bootstrap_Ext_AppClassLoader
BootstrapClassLoader 的加载路径:
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/sunrsasign.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/classes
----------------------------
sun.misc.Launcher$ExtClassLoader@306a30c7
扩展类加载器 的加载路径:
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar
file:/System/Library/Java/Extensions/MRJToolkit.jar
----------------------------
sun.misc.Launcher$AppClassLoader@18b4aac2
应用(系统)类加载器 的加载路径:
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar
file:/development/runtime-environment/repository/com/weiease/rdc/rdc-common/1.0.9/rdc-common-1.0.9.jar
file:/development/runtime-environment/repository/org/springframework/spring-context/5.2.0.RELEASE/spring-context-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/spring-aop/5.2.0.RELEASE/spring-aop-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/spring-beans/5.2.0.RELEASE/spring-beans-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/spring-expression/5.2.0.RELEASE/spring-expression-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/slf4j/slf4j-api/1.7.9/slf4j-api-1.7.9.jar
file:/development/runtime-environment/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar
file:/development/runtime-environment/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar
file:/development/runtime-environment/repository/org/springframework/data/spring-data-commons/2.3.4.RELEASE/spring-data-commons-2.3.4.RELEASE.jar
file:/development/runtime-environment/repository/org/projectlombok/lombok/1.18.10/lombok-1.18.10.jar
file:/development/runtime-environment/repository/org/springframework/spring-core/5.2.0.RELEASE/spring-core-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/spring-jcl/5.2.0.RELEASE/spring-jcl-5.2.0.RELEASE.jar
file:/development/runtime-environment/repository/org/reflections/reflections/0.9.10/reflections-0.9.10.jar
file:/development/runtime-environment/repository/com/google/guava/guava/22.0/guava-22.0.jar
file:/development/runtime-environment/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar
file:/development/runtime-environment/repository/com/google/errorprone/error_prone_annotations/2.0.18/error_prone_annotations-2.0.18.jar
file:/development/runtime-environment/repository/com/google/j2objc/j2objc-annotations/1.1/j2objc-annotations-1.1.jar
file:/development/runtime-environment/repository/org/codehaus/mojo/animal-sniffer-annotations/1.14/animal-sniffer-annotations-1.14.jar
file:/development/runtime-environment/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar
file:/development/runtime-environment/repository/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1.jar
file:/development/runtime-environment/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar
file:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-starter/2.7.4.1/dubbo-spring-boot-starter-2.7.4.1.jar
file:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-autoconfigure/2.7.4.1/dubbo-spring-boot-autoconfigure-2.7.4.1.jar
file:/development/runtime-environment/repository/org/apache/dubbo/dubbo-spring-boot-autoconfigure-compatible/2.7.4.1/dubbo-spring-boot-autoconfigure-compatible-2.7.4.1.jar
file:/development/runtime-environment/repository/org/apache/dubbo/dubbo/2.7.4.1/dubbo-2.7.4.1.jar
file:/development/runtime-environment/repository/io/netty/netty-all/4.1.25.Final/netty-all-4.1.25.Final.jar
file:/development/runtime-environment/repository/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar
file:/development/runtime-environment/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-discovery/2.2.3.RELEASE/spring-cloud-starter-alibaba-nacos-discovery-2.2.3.RELEASE.jar
file:/development/runtime-environment/repository/com/alibaba/nacos/nacos-client/1.3.3/nacos-client-1.3.3.jar
file:/development/runtime-environment/repository/com/alibaba/nacos/nacos-common/1.3.3/nacos-common-1.3.3.jar
file:/development/runtime-environment/repository/commons-io/commons-io/2.2/commons-io-2.2.jar
file:/development/runtime-environment/repository/org/apache/httpcomponents/httpasyncclient/4.1.3/httpasyncclient-4.1.3.jar
file:/development/runtime-environment/repository/org/apache/httpcomponents/httpcore-nio/4.4.6/httpcore-nio-4.4.6.jar
file:/development/runtime-environment/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar
file:/development/runtime-environment/repository/com/alibaba/nacos/nacos-api/1.3.3/nacos-api-1.3.3.jar
file:/development/runtime-environment/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar
file:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-core/2.10.4/jackson-core-2.10.4.jar
file:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-databind/2.10.4/jackson-databind-2.10.4.jar
file:/development/runtime-environment/repository/com/fasterxml/jackson/core/jackson-annotations/2.10.4/jackson-annotations-2.10.4.jar
file:/development/runtime-environment/repository/io/prometheus/simpleclient/0.5.0/simpleclient-0.5.0.jar
file:/development/runtime-environment/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar
file:/development/runtime-environment/repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-commons/2.2.5.RELEASE/spring-cloud-commons-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/security/spring-security-crypto/5.3.3.RELEASE/spring-security-crypto-5.3.3.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-context/2.2.5.RELEASE/spring-cloud-context-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter-netflix-ribbon/2.2.5.RELEASE/spring-cloud-starter-netflix-ribbon-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter/2.2.5.RELEASE/spring-cloud-starter-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/boot/spring-boot-starter/2.3.2.RELEASE/spring-boot-starter-2.3.2.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/boot/spring-boot/2.3.2.RELEASE/spring-boot-2.3.2.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/boot/spring-boot-autoconfigure/2.3.2.RELEASE/spring-boot-autoconfigure-2.3.2.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/boot/spring-boot-starter-logging/2.3.2.RELEASE/spring-boot-starter-logging-2.3.2.RELEASE.jar
file:/development/runtime-environment/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar
file:/development/runtime-environment/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar
file:/development/runtime-environment/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar
file:/development/runtime-environment/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar
file:/development/runtime-environment/repository/org/springframework/security/spring-security-rsa/1.0.9.RELEASE/spring-security-rsa-1.0.9.RELEASE.jar
file:/development/runtime-environment/repository/org/bouncycastle/bcpkix-jdk15on/1.64/bcpkix-jdk15on-1.64.jar
file:/development/runtime-environment/repository/org/bouncycastle/bcprov-jdk15on/1.64/bcprov-jdk15on-1.64.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-netflix-ribbon/2.2.5.RELEASE/spring-cloud-netflix-ribbon-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-netflix-archaius/2.2.5.RELEASE/spring-cloud-netflix-archaius-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/org/springframework/cloud/spring-cloud-starter-netflix-archaius/2.2.5.RELEASE/spring-cloud-starter-netflix-archaius-2.2.5.RELEASE.jar
file:/development/runtime-environment/repository/com/netflix/archaius/archaius-core/0.7.6/archaius-core-0.7.6.jar
file:/development/runtime-environment/repository/commons-configuration/commons-configuration/1.8/commons-configuration-1.8.jar
file:/development/runtime-environment/repository/com/netflix/ribbon/ribbon/2.3.0/ribbon-2.3.0.jar
file:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-transport/2.3.0/ribbon-transport-2.3.0.jar
file:/development/runtime-environment/repository/io/reactivex/rxnetty-contexts/0.4.9/rxnetty-contexts-0.4.9.jar
file:/development/runtime-environment/repository/io/reactivex/rxnetty-servo/0.4.9/rxnetty-servo-0.4.9.jar
file:/development/runtime-environment/repository/com/netflix/hystrix/hystrix-core/1.4.3/hystrix-core-1.4.3.jar
file:/development/runtime-environment/repository/javax/inject/javax.inject/1/javax.inject-1.jar
file:/development/runtime-environment/repository/io/reactivex/rxnetty/0.4.9/rxnetty-0.4.9.jar
file:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-core/2.3.0/ribbon-core-2.3.0.jar
file:/development/runtime-environment/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
file:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-httpclient/2.3.0/ribbon-httpclient-2.3.0.jar
file:/development/runtime-environment/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar
file:/development/runtime-environment/repository/com/sun/jersey/jersey-client/1.19.1/jersey-client-1.19.1.jar
file:/development/runtime-environment/repository/com/sun/jersey/jersey-core/1.19.1/jersey-core-1.19.1.jar
file:/development/runtime-environment/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar
file:/development/runtime-environment/repository/com/sun/jersey/contribs/jersey-apache-client4/1.19.1/jersey-apache-client4-1.19.1.jar
file:/development/runtime-environment/repository/com/netflix/servo/servo-core/0.10.1/servo-core-0.10.1.jar
file:/development/runtime-environment/repository/com/netflix/servo/servo-internal/0.10.1/servo-internal-0.10.1.jar
file:/development/runtime-environment/repository/com/netflix/netflix-commons/netflix-commons-util/0.1.1/netflix-commons-util-0.1.1.jar
file:/development/runtime-environment/repository/com/netflix/ribbon/ribbon-loadbalancer/2.3.0/ribbon-loadbalancer-2.3.0.jar
file:/development/runtime-environment/repository/com/netflix/netflix-commons/netflix-statistics/0.1.1/netflix-statistics-0.1.1.jar
file:/development/runtime-environment/repository/io/reactivex/rxjava/1.3.8/rxjava-1.3.8.jar
file:/development/runtime-environment/repository/org/aspectj/aspectjweaver/1.8.6/aspectjweaver-1.8.6.jar
file:/development/runtime-environment/repository/com/alibaba/fastjson/1.2.48/fastjson-1.2.48.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-connector-basic/1.0.2.v20150114/aether-connector-basic-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-api/1.0.2.v20150114/aether-api-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-spi/1.0.2.v20150114/aether-spi-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-util/1.0.2.v20150114/aether-util-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-file/1.0.2.v20150114/aether-transport-file-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-http/1.0.2.v20150114/aether-transport-http-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/apache/httpcomponents/httpclient/4.5/httpclient-4.5.jar
file:/development/runtime-environment/repository/org/apache/httpcomponents/httpcore/4.4.1/httpcore-4.4.1.jar
file:/development/runtime-environment/repository/org/slf4j/jcl-over-slf4j/1.6.2/jcl-over-slf4j-1.6.2.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-transport-wagon/1.0.2.v20150114/aether-transport-wagon-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-provider-api/1.0/wagon-provider-api-1.0.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-aether-provider/3.3.9/maven-aether-provider-3.3.9.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-model/3.3.9/maven-model-3.3.9.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-model-builder/3.3.9/maven-model-builder-3.3.9.jar
file:/development/runtime-environment/repository/org/codehaus/plexus/plexus-interpolation/1.21/plexus-interpolation-1.21.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-artifact/3.3.9/maven-artifact-3.3.9.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-builder-support/3.3.9/maven-builder-support-3.3.9.jar
file:/development/runtime-environment/repository/org/apache/maven/maven-repository-metadata/3.3.9/maven-repository-metadata-3.3.9.jar
file:/development/runtime-environment/repository/org/eclipse/aether/aether-impl/1.0.2.v20150114/aether-impl-1.0.2.v20150114.jar
file:/development/runtime-environment/repository/org/codehaus/plexus/plexus-component-annotations/1.6/plexus-component-annotations-1.6.jar
file:/development/runtime-environment/repository/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar
file:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-ssh/1.0/wagon-ssh-1.0.jar
file:/development/runtime-environment/repository/com/jcraft/jsch/0.1.44-1/jsch-0.1.44-1.jar
file:/development/runtime-environment/repository/org/apache/maven/wagon/wagon-ssh-common/1.0/wagon-ssh-common-1.0.jar
file:/development/runtime-environment/repository/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-6/plexus-interactivity-api-1.0-alpha-6.jar
file:/Applications/IntelliJ%20IDEA.app/Contents/lib/idea_rt.jar
三 通过上面执行的结果我们可以得知虚拟机不同的类加载器有不同的加载路径
- BootstrapClassLoader ---加载jre/目录下的核心库
- ExtClassLoader扩展类加载器 ---加载/jre/lib/ext/目录下的扩展包
- AppClassLoader应用(系统)类加载器 ---加载CLASSPATH路径下的包和maven依赖的库类
Java 类加载双亲委托原理
双亲委派模型
从Java虚拟机的角度来讲,只存在两种不同的类加载器:一种是启动类加载器(Bootstrap ClassLoader),这个类加载器使用C++语言实现,是虚拟机自身的一部分;另一种就是所有其他的类加载器,这些类加载器都由Java语言实现,独立于虚拟机外部,并且全都继承自抽象类java.lang.ClassLoader。
从Java开发人员的角度来看, 类加载器还可以划分得更细致一些, 绝大部分Java程序都会使用到以下3种系统提供的类加载器。
1)启动类加载器(Bootstrap ClassLoader):前面已经介绍过,这个类加载器负责将存放在<JAVA_HOME>\lib目录中的,或者被-Xbootclasspath参数所指定的路径中的,并且是虚拟机识别的(仅按照文件名识别,如rt.jar,名字不符合的类库即使放在lib目录中也不会被加载)类库加载到虚拟机内存中。启动类加载器无法被Java程序直接引用。
2)扩展类加载器(Extension ClassLoader):这个加载器由sun.misc.Launcher.ExtClassLoader实现,它负责加载<JAVA_HOME>\lib\ext目录中的,或者被java.ext.dirs系统变量所指定的路径中的所有类库,开发者可以直接使用扩展类加载器。
3)应用程序类加载器(Application ClassLoader):这个类加载器由sun.misc.Launcher.AppClassLoader实现。由于这个类加载器是ClassLoader中的getSystemClassLoader()方法的返回值,所以一般也称它为系统类加载器。它负责加载用户类路径(Class Path)上所指定的类库,开发者可以直接使用这个类加载器,如果应用程序中没有自定义过自己的类加载器,一般情况下这个就是程序中默认的类加载器。
我们的应用程序都是由这3种类加载器互相配合进行加载的,如果有必要,还可以加入自己定义的类加载器。这些类加载器之间的关系一般如下图所示。
图中展示的类加载器之间的这种层次关系,称为类加载器的双亲委派模型(Parents Delegation Model)。双亲委派模型要求除了顶层的启动类加载器外,其余的类加载器都应当有自己的父类加载器。这里类加载器之间的父子关系一般不会以继承(Inheritance)的关系来实现,而是都使用组合(Composition)关系来复用父加载器的代码。类加载器的双亲委派模型在JDK 1.2期间被引入并被广泛应用于之后几乎所有的Java程序中,但它并不是一个强制性的约束模型,而是Java设计者推荐给开发者的一种类加载器实现方式。
双亲委派模型的工作过程是:如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每一个层次的类加载器都是如此,因此所有的加载请求最终都应该传送到顶层的启动类加载器中,只有当父加载器反馈自己无法完成这个加载请求(它的搜索范围中没有找到所需的类)时,子加载器才会尝试自己去加载。
使用双亲委派模型来组织类加载器之间的关系,有一个显而易见的好处就是Java类随着它的类加载器一起具备了一种带有优先级的层次关系。例如类java.lang.Object,它存放在rt.jar之中,无论哪一个类加载器要加载这个类,最终都是委派给处于模型最顶端的启动类加载器进行加载,因此Object类在程序的各种类加载器环境中都是同一个类。相反,如果没有使用双亲委派模型,由各个类加载器自行去加载的话,如果用户自己编写了一个称为java.lang.Object的类,并放在程序的Class Path中,那系统中将会出现多个不同的Object类,Java类型体系中最基础的行为也就无法保证,应用程序也将会变得一片混乱。如果读者有兴趣的话,可以尝试去编写一个与rt.jar类库中已有类重名的Java类,将会发现可以正常编译,但永远无法被加载运行。
双亲委派模型对于保证Java程序的稳定运作很重要,但它的实现却非常简单,实现双亲委派的代码都集中在java.lang.ClassLoader的loadClass()方法之中,如以下代码所示,逻辑清晰易懂:
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
//首先检查请求的类是否已经被加载过了
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
//如果父类加载器抛出ClassNotFoundException,说明父类加载器无法完成加载请求
}
if (c == null) {
//在父类加载器无法加载的时候,再调用本身的findClass方法来进行类加载
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
ClassLoader.java
1:先调用parent.loadClass(name, false)
2:如果父亲加载失败,则调用 loadClass(name, false) 方法 查找"当前类加载器"对应的"目录"来加载
public Class<?>loadClass(String name, boolean resolve)
此方法的默认实现将按以下顺序搜索类:
1.调用 findLoadedClass(name) 来检查是否已经加载类。
2.在父类加载器上调用 loadClass方法。如果父类加载器为 null,则使用虚拟机的内置类加载器。
3.调用 findClass(name) 方法查找类。
二 自定义类加载器委托机制
PS: 如果用户想从自定义的目录下加载类(.class文件),则可以自定义类加载器: