关于classloader:jvm的ApplicationClassLoader如何获取的Classpath

51次阅读

共计 8587 个字符,预计需要花费 22 分钟才能阅读完成。

参考文献: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 的对话:

重点:

我也不晓得 chatGPT 有没有胡说,所以,如有谬误请麻烦斧正,谢谢。

正文完
 0