jvm的ApplicationClassLoader如何获取的Classpath

参考文献:https://blog.csdn.net/loreal8/article/details/106572817

我最近在看springBoot源码,其中关于SpringFactoriesLoader::loadSpringFactories使用classLoader加载META-INF/spring.factories里面写的类。我看META-INF/spring.factories是相对路径,所以就想知道到底是从哪个根目录下找META-INF/spring.factories

经过我问chatGPT和使用google搜索,得知是在Classpath下进行搜索的。以下是详情:

在使用shell,执行 java 命令时,java应用作为shell的子进程,继承了shell进程的当前工作目录,并把当前工作目录加进Classpath集合。

子进程的继承:

fork出子进程后,子进程都会继承父进程以下信息;
1.文件描述符
2.实际用户ID,实际组ID,有效用户ID,有效组ID
3.进程组ID
4.添加组ID
5.对话期ID
6.控制终端
7.设置-用户-ID标志和设置-组-ID标志
8.当前工作目录
9.根目录
10.文件方式创建字
11.信号屏蔽和排列
12.对任意打开文件描述符大的在执行时关闭标志。
13.环境
14.链接的共享存储段
15.资源限制

classLoader在加载META-INF/spring.factories时,会通过jvm的方法System.getProperty("java.class.path"),获取到calsspath,然后以classpath为根目录进行搜索。

import org.junit.jupiter.api.Test;

public class TestSomething {
    @Test
    public void testClass() {

        String classpath = System.getProperty("java.class.path");
        System.out.println("获取Classpath为:" + classpath);
    }
}

输出为:

获取Classpath为:/home/jo/.m2/repository/org/junit/platform/junit-platform-launcher/1.8.2/junit-platform-launcher-1.8.2.jar:/home/jo/.m2/repository/org/junit/vintage/junit-vintage-engine/5.8.2/junit-vintage-engine-5.8.2.jar:/home/jo/APP/idea-IC-231.9011.34/lib/idea_rt.jar:/home/jo/APP/idea-IC-231.9011.34/plugins/junit/lib/junit5-rt.jar:/home/jo/APP/idea-IC-231.9011.34/plugins/junit/lib/junit-rt.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/charsets.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/deploy.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/cldrdata.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/dnsns.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/jaccess.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/jfxrt.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/localedata.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/nashorn.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/sunec.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/sunjce_provider.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/sunpkcs11.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/ext/zipfs.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/javaws.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/jce.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/jfr.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/jfxswt.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/jsse.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/management-agent.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/plugin.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/resources.jar:/usr/share/jdk/jdk8/jdk1.8.0_241/jre/lib/rt.jar:/home/jo/IdeaProjects/webDemo/target/test-classes:/home/jo/IdeaProjects/webDemo/target/classes:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.7.4/spring-boot-starter-web-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter/2.7.4/spring-boot-starter-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot/2.7.4/spring-boot-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.7.4/spring-boot-autoconfigure-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.7.4/spring-boot-starter-logging-2.7.4.jar:/home/jo/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar:/home/jo/.m2/repository/ch/qos/logback/logback-core/1.2.11/logback-core-1.2.11.jar:/home/jo/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.17.2/log4j-to-slf4j-2.17.2.jar:/home/jo/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar:/home/jo/.m2/repository/org/slf4j/jul-to-slf4j/1.7.36/jul-to-slf4j-1.7.36.jar:/home/jo/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/home/jo/.m2/repository/org/yaml/snakeyaml/1.30/snakeyaml-1.30.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.7.4/spring-boot-starter-json-2.7.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.13.4/jackson-databind-2.13.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.13.4/jackson-annotations-2.13.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.13.4/jackson-core-2.13.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.13.4/jackson-datatype-jdk8-2.13.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.13.4/jackson-datatype-jsr310-2.13.4.jar:/home/jo/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.13.4/jackson-module-parameter-names-2.13.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.7.4/spring-boot-starter-tomcat-2.7.4.jar:/home/jo/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.65/tomcat-embed-core-9.0.65.jar:/home/jo/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.65/tomcat-embed-el-9.0.65.jar:/home/jo/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.65/tomcat-embed-websocket-9.0.65.jar:/home/jo/.m2/repository/org/springframework/spring-web/5.3.23/spring-web-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-beans/5.3.23/spring-beans-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-webmvc/5.3.23/spring-webmvc-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-aop/5.3.23/spring-aop-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-context/5.3.23/spring-context-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-expression/5.3.23/spring-expression-5.3.23.jar:/home/jo/.m2/repository/org/projectlombok/lombok/1.18.24/lombok-1.18.24.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-starter-test/2.7.4/spring-boot-starter-test-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-test/2.7.4/spring-boot-test-2.7.4.jar:/home/jo/.m2/repository/org/springframework/boot/spring-boot-test-autoconfigure/2.7.4/spring-boot-test-autoconfigure-2.7.4.jar:/home/jo/.m2/repository/com/jayway/jsonpath/json-path/2.7.0/json-path-2.7.0.jar:/home/jo/.m2/repository/net/minidev/json-smart/2.4.8/json-smart-2.4.8.jar:/home/jo/.m2/repository/net/minidev/accessors-smart/2.4.8/accessors-smart-2.4.8.jar:/home/jo/.m2/repository/org/ow2/asm/asm/9.1/asm-9.1.jar:/home/jo/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar:/home/jo/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/2.3.3/jakarta.xml.bind-api-2.3.3.jar:/home/jo/.m2/repository/jakarta/activation/jakarta.activation-api/1.2.2/jakarta.activation-api-1.2.2.jar:/home/jo/.m2/repository/org/assertj/assertj-core/3.22.0/assertj-core-3.22.0.jar:/home/jo/.m2/repository/org/hamcrest/hamcrest/2.2/hamcrest-2.2.jar:/home/jo/.m2/repository/org/mockito/mockito-core/4.5.1/mockito-core-4.5.1.jar:/home/jo/.m2/repository/net/bytebuddy/byte-buddy/1.12.17/byte-buddy-1.12.17.jar:/home/jo/.m2/repository/net/bytebuddy/byte-buddy-agent/1.12.17/byte-buddy-agent-1.12.17.jar:/home/jo/.m2/repository/org/objenesis/objenesis/3.2/objenesis-3.2.jar:/home/jo/.m2/repository/org/mockito/mockito-junit-jupiter/4.5.1/mockito-junit-jupiter-4.5.1.jar:/home/jo/.m2/repository/org/skyscreamer/jsonassert/1.5.1/jsonassert-1.5.1.jar:/home/jo/.m2/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar:/home/jo/.m2/repository/org/springframework/spring-core/5.3.23/spring-core-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-jcl/5.3.23/spring-jcl-5.3.23.jar:/home/jo/.m2/repository/org/springframework/spring-test/5.3.23/spring-test-5.3.23.jar:/home/jo/.m2/repository/org/xmlunit/xmlunit-core/2.9.0/xmlunit-core-2.9.0.jar:/home/jo/.m2/repository/junit/junit/4.12/junit-4.12.jar:/home/jo/.m2/repository/org/hamcrest/hamcrest-core/2.2/hamcrest-core-2.2.jar:/home/jo/.m2/repository/com/alibaba/fastjson/1.2.76/fastjson-1.2.76.jar:/home/jo/.m2/repository/org/junit/jupiter/junit-jupiter/5.10.0-M1/junit-jupiter-5.10.0-M1.jar:/home/jo/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.8.2/junit-jupiter-api-5.8.2.jar:/home/jo/.m2/repository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar:/home/jo/.m2/repository/org/junit/platform/junit-platform-commons/1.8.2/junit-platform-commons-1.8.2.jar:/home/jo/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar:/home/jo/.m2/repository/org/junit/jupiter/junit-jupiter-params/5.8.2/junit-jupiter-params-5.8.2.jar:/home/jo/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.8.2/junit-jupiter-engine-5.8.2.jar:/home/jo/.m2/repository/org/junit/platform/junit-platform-engine/1.8.2/junit-platform-engine-1.8.2.jar:/home/jo/APP/idea-IC-231.9011.34/lib/idea_rt.jar

以下是我和chatGPT的对话:

jvm的ApplicationClassLoader如何获取的Classpath_第1张图片

jvm的ApplicationClassLoader如何获取的Classpath_第2张图片

jvm的ApplicationClassLoader如何获取的Classpath_第3张图片

jvm的ApplicationClassLoader如何获取的Classpath_第4张图片

重点:
jvm的ApplicationClassLoader如何获取的Classpath_第5张图片

jvm的ApplicationClassLoader如何获取的Classpath_第6张图片

我也不知道chatGPT有没有胡说,所以,如有错误请麻烦指正,谢谢。

你可能感兴趣的:(jvm的ApplicationClassLoader如何获取的Classpath)