Featured image of post Java默认类加载器的加载路径

Java默认类加载器的加载路径

一些栗子理解类加载器的加载路径

前文有提到Java的默认ClassLoader有三种,且三种的ClassLoader会加载不同的Java类。

他们加载的类的路径在哪?

是否可以进行设置?

本文做一个初步探讨

全量输出

在不配置任何参数的情况下,执行MainTest.java查看默认路径

MainTest.java内容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.net.URL;
import java.net.URLClassLoader;

public class MainTest {
    public static void main(String[] args) {
        System.out.println("BootstrapClassLoader LoadPath: ");

        URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
        for(URL url : urls)
            System.out.println(url);
        // System.out.println(System.getProperty("sun.boot.class.path")); 也可以
        System.out.println("----------------------------");
	
        //取得扩展类加载器
        URLClassLoader extClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader().getParent();

        System.out.println(extClassLoader);
        System.out.println("ExtClassLoader LoadPath: ");

        urls = extClassLoader.getURLs();
        for(URL url : urls)
            System.out.println(url);
		// System.out.println(System.getProperty("java.ext.dirs")); 也可以
        System.out.println("----------------------------");

        //取得应用(系统)类加载器
        URLClassLoader appClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();

        System.out.println(appClassLoader);
        System.out.println("AppClassLoader LoadPath: ");

        urls = appClassLoader.getURLs();
        for(URL url : urls)
            System.out.println(url);
		// System.out.println(System.getProperty("java.class.path")); 也可以
        System.out.println("----------------------------");
    }
}

使用最原始的方法运行,结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
cmd>javac.exe .\MainTest.java
# 有一个warning,删除了,不重要,jdk version 如下
# java version "1.8.0_181"
# Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
# Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

# 此时的目录结构
# ./
# ├── MainTest.class
# └── MainTest.java

cmd>java.exe MainTest
# 全量输出如下
BootstrapClassLoader LoadPath:
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/resources.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/sunrsasign.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jsse.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jce.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/charsets.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfr.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/classes
----------------------------
sun.misc.Launcher$ExtClassLoader@15db9742
ExtClassLoader LoadPath:
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/access-bridge-64.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/cldrdata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/dnsns.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jaccess.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jfxrt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/localedata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/nashorn.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunec.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunjce_provider.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunmscapi.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunpkcs11.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/zipfs.jar
----------------------------
sun.misc.Launcher$AppClassLoader@73d16e93
AppClassLoader LoadPath:
file:/C:/Users/Dan/Desktop/ppppppp/
----------------------------

Class Loader

  1. Bootstrap Class Loader:加载sun.boot.class.path所指定的路径或jar

默认情况下,可以看到加载的类集中在jre\lib目录和jre\classes目录下。

  1. Extension Class Loader:加载java.ext.dirs所指定的路径或jar。

默认情况下,可以看到加载的类集中在jre\lib\ext目录下。

  1. Application Class Loader加载java.class.path并会搜索应用程序的类路径(包括用户定义的类路径和系统类路径)

默认情况下,最简单的情况,可以看到加载的类只有java命令的执行目录。也就是ClassPath的默认路径,相关知识链接

进阶一点

可以看到三个类加载的默认路径还是挺单一的,所以如果我们想要增加类加载器的加载路径,应该如何做呢?

bootclasspath:对应 Bootstrap CLassLoader。java虚拟机系统参数sun.boot.class.path。 Extensions JAR files:对应 Extention ClassLoader 。java虚拟机系统参数java.ext.dirs。 classpath:对应 AppClassLoader。java虚拟机系统参数java.class.path

Bootstrap Class Loader

如果要修改或者增加Bootstrap Class Loader的加载路径,三个参数

  • -Xbootclasspath: 完全取代基本核心的Java class 搜索路径.不常用(慎用),否则要重新写所有Java核心类
  • -Xbootclasspath/a: 后缀。在bootclasspath后面添加。常用!!
  • -Xbootclasspath/p: 前缀。在bootclasspath前面添加。不常用,避免引起不必要的冲突。

-Xbootclasspath/a 示例

省略无关部分,结果如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cmd>java.exe -Xbootclasspath/a:.\lib\ MainTest
BootstrapClassLoader LoadPath:
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/resources.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/sunrsasign.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jsse.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jce.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/charsets.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfr.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/classes
file:/C:/Users/Dan/Desktop/ppppppp/lib

可以看到最后一行添加了当前路径下的lib文件夹

-Xbootclasspath/p 示例

省略无关部分,结果如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cmd>java.exe -Xbootclasspath/p:.\lib\ MainTest
BootstrapClassLoader LoadPath:
file:/C:/Users/Dan/Desktop/ppppppp/lib
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/resources.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/sunrsasign.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jsse.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jce.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/charsets.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfr.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/classes

可以看到第一行添加了当前路径下的lib文件夹

Tips

  • 可以添加.class文件目录
  • 可以添加.yml.xml.properties(或其它配置文件)文件目录
  • 可以添加.jar文件
  • 不可以添加.jar文件所在目录

Extension Class Loader

  • java -Djava.ext.dirs=...即可添加

-Djava.ext.dirs 示例

需要使用System.out.println(System.getProperty("java.ext.dirs"))来输出,观察变化

1
2
3
        System.out.println("ExtClassLoader LoadPath: ");
		System.out.println(System.getProperty("java.ext.dirs"));
        System.out.println("----------------------------");

省略无关部分,结果如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
cmd>java.exe MainTest
# 默认结果 C:\Windows\Sun\Java\lib\ext 不存在该目录
sun.misc.Launcher$ExtClassLoader@15db9742
ExtClassLoader LoadPath:
D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext;C:\Windows\Sun\Java\lib\ext


cmd>java.exe -Djava.ext.dirs=.\test MainTest
# 设置后的结果,只剩下了设置目录
sun.misc.Launcher$ExtClassLoader@15db9742
ExtClassLoader LoadPath:
.\test


cmd>java.exe -Djava.ext.dirs="%JAVA_HOME%jre\lib\ext;.\target" MainTest
# 推荐设置结果,包含了默认结果中的重要部分和自定义部分
sun.misc.Launcher$ExtClassLoader@15db9742
ExtClassLoader LoadPath:
D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext;.\target

对栗子的解释

  • -Djava.ext.dirs="%JAVA_HOME%jre\lib\ext;.\target"加引号是因为%JAVA_HOME%包含空格。
  • 尽量将java自带的lib\ext目录带上,即%JAVA_HOME%jre\lib\ext。否则,在用其下.jar时,会发生错误。如果确定不需要其下.jar时,则不用加上java自带的lib\ext目录。
  • 缺少java自带的lib\ext目录时,java.security包不能使用(NoSuchAlgorithmException)。

Tips

  • 不可以添加.class文件目录
  • 不可以添加.jar文件
  • 可以添加.jar文件所在目录
  • 推荐将java自带的lib\ext目录带上

Application Class Loader

  • java -classpath ...
  • java -cp ...

注:java -cp ...java -classpath ... 的简写

-classpath 示例

省略无关部分,结果如下 在这篇文章中也有用到

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cmd>java.exe -cp .\ttttest MainTest
# 因为-cp参数被覆盖,默认有当前路径 . 但是覆盖后只有 .\ttttest ,不在当前路径就找不到字节码文件,所以报错
错误: 找不到或无法加载主类 MainTest

cmd>java.exe -cp .;.\ttttest MainTest
# 多个路径以;符号为分隔,.表示当前路径,添加后报错解除
sun.misc.Launcher$AppClassLoader@73d16e93
AppClassLoader LoadPath:
file:/C:/Users/Dan/Desktop/ppppppp/
file:/C:/Users/Dan/Desktop/ppppppp/ttttest
----------------------------

可以看到最后一行添加了ttttest文件夹

如果参数为.\ttttest;.则结果为

1
2
3
4
5
6
cmd>java.exe -cp .\ttttest;. MainTest
sun.misc.Launcher$AppClassLoader@73d16e93
AppClassLoader LoadPath:
file:/C:/Users/Dan/Desktop/ppppppp/ttttest
file:/C:/Users/Dan/Desktop/ppppppp/
----------------------------

可以看到第一行添加了ttttest文件夹

Tips

  • 鸡粒推荐!!加入当前文件路径

  • 可以添加.class文件目录

  • 可以添加.jar文件

  • 不可以添加.jar文件所在目录

  • 可以通配符* java -cp target\* test.Test target\*是个特例,可以让java自动搜索其下的.jar文件,并加载。

IDEA 结果一探

代码放入IDEA进行执行,会发现结果不同

IDEA结果如下

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
BootstrapClassLoader LoadPath: 
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/resources.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/sunrsasign.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jsse.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jce.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/charsets.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfr.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/classes
----------------------------
sun.misc.Launcher$ExtClassLoader@3eb07fd3
ExtClassLoader LoadPath: 
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/access-bridge-64.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/cldrdata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/dnsns.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jaccess.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jfxrt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/localedata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/nashorn.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunec.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunjce_provider.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunmscapi.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunpkcs11.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/zipfs.jar
----------------------------
sun.misc.Launcher$AppClassLoader@18b4aac2
AppClassLoader LoadPath: 
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/charsets.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/deploy.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/access-bridge-64.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/cldrdata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/dnsns.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jaccess.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/jfxrt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/localedata.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/nashorn.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunec.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunjce_provider.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunmscapi.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/sunpkcs11.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/ext/zipfs.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/javaws.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jce.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfr.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jfxswt.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/jsse.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/management-agent.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/plugin.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/resources.jar
file:/D:/Program%20Files/Java/jdk1.8.0_181/jre/lib/rt.jar
file:/D:/Users/Dan/Code/Java/Empty/target/classes/
file:/C:/Users/Dan/.m2/repository/org/reflections/reflections/0.9.9/reflections-0.9.9.jar
file:/C:/Users/Dan/.m2/repository/com/google/guava/guava/15.0/guava-15.0.jar
file:/C:/Users/Dan/.m2/repository/com/google/code/findbugs/annotations/2.0.1/annotations-2.0.1.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-api/2.2.6/shrinkwrap-resolver-api-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-spi/2.2.6/shrinkwrap-resolver-spi-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-api-maven/2.2.6/shrinkwrap-resolver-api-maven-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-spi-maven/2.2.6/shrinkwrap-resolver-spi-maven-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-api-maven-archive/2.2.6/shrinkwrap-resolver-api-maven-archive-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-api/1.2.6/shrinkwrap-api-1.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-impl-maven/2.2.6/shrinkwrap-resolver-impl-maven-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-api/1.0.0.v20140518/aether-api-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-impl/1.0.0.v20140518/aether-impl-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-spi/1.0.0.v20140518/aether-spi-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-util/1.0.0.v20140518/aether-util-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-connector-basic/1.0.0.v20140518/aether-connector-basic-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/aether/aether-transport-wagon/1.0.0.v20140518/aether-transport-wagon-1.0.0.v20140518.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-aether-provider/3.2.5/maven-aether-provider-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-model/3.2.5/maven-model-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-model-builder/3.2.5/maven-model-builder-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-repository-metadata/3.2.5/maven-repository-metadata-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-settings/3.2.5/maven-settings-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/maven-settings-builder/3.2.5/maven-settings-builder-3.2.5.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.21/plexus-interpolation-1.21.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar
file:/C:/Users/Dan/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
file:/C:/Users/Dan/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/wagon/wagon-provider-api/2.6/wagon-provider-api-2.6.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/wagon/wagon-file/2.6/wagon-file-2.6.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/wagon/wagon-http-lightweight/2.6/wagon-http-lightweight-2.6.jar
file:/C:/Users/Dan/.m2/repository/org/apache/maven/wagon/wagon-http-shared/2.6/wagon-http-shared-2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-impl-maven-archive/2.2.6/shrinkwrap-resolver-impl-maven-archive-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-impl-base/1.2.6/shrinkwrap-impl-base-1.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-spi/1.2.6/shrinkwrap-spi-1.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-spi-maven-archive/2.2.6/shrinkwrap-resolver-spi-maven-archive-2.2.6.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/sisu/org.eclipse.sisu.plexus/0.3.0.M1/org.eclipse.sisu.plexus-0.3.0.M1.jar
file:/C:/Users/Dan/.m2/repository/org/eclipse/sisu/org.eclipse.sisu.inject/0.3.0.M1/org.eclipse.sisu.inject-0.3.0.M1.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/plexus/plexus-compiler-javac/2.3/plexus-compiler-javac-2.3.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.3/plexus-compiler-api-2.3.jar
file:/C:/Users/Dan/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar
file:/C:/Users/Dan/.m2/repository/com/nqzero/permit-reflect/0.3/permit-reflect-0.3.jar
file:/C:/Users/Dan/.m2/repository/commons-codec/commons-codec/1.9/commons-codec-1.9.jar
file:/C:/Users/Dan/.m2/repository/commons-io/commons-io/2.6/commons-io-2.6.jar
file:/C:/Users/Dan/.m2/repository/org/jenkins-ci/main/remoting/2.55/remoting-2.55.jar
file:/C:/Users/Dan/.m2/repository/org/jenkins-ci/constant-pool-scanner/1.2/constant-pool-scanner-1.2.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/remoting/jboss-remoting/4.0.19.Final/jboss-remoting-4.0.19.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/xnio/xnio-api/3.3.4.Final/xnio-api-3.3.4.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/jboss-common-core/2.5.0.Final/jboss-common-core-2.5.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/xnio/xnio-nio/3.3.4.Final/xnio-nio-3.3.4.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/sasl/jboss-sasl/1.0.5.Final/jboss-sasl-1.0.5.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/remotingjmx/remoting-jmx/2.0.1.Final/remoting-jmx-2.0.1.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/logging/jboss-logging-processor/1.2.0.Final/jboss-logging-processor-1.2.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/jdeparser/jdeparser/1.0.0.Final/jdeparser-1.0.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/marshalling/jboss-marshalling/1.4.10.Final/jboss-marshalling-1.4.10.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/marshalling/jboss-marshalling-river/1.4.10.Final/jboss-marshalling-river-1.4.10.Final.jar
file:/C:/Users/Dan/.m2/repository/commons-collections/commons-collections/3.1/commons-collections-3.1.jar
file:/C:/Users/Dan/.m2/repository/org/beanshell/bsh/2.0b5/bsh-2.0b5.jar
file:/C:/Users/Dan/.m2/repository/commons-beanutils/commons-beanutils/1.9.2/commons-beanutils-1.9.2.jar
file:/C:/Users/Dan/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
file:/C:/Users/Dan/.m2/repository/org/apache/commons/commons-collections4/4.0/commons-collections4-4.0.jar
file:/C:/Users/Dan/.m2/repository/org/codehaus/groovy/groovy/2.3.9/groovy-2.3.9.jar
file:/C:/Users/Dan/.m2/repository/org/springframework/spring-core/4.1.4.RELEASE/spring-core-4.1.4.RELEASE.jar
file:/C:/Users/Dan/.m2/repository/org/springframework/spring-beans/4.1.4.RELEASE/spring-beans-4.1.4.RELEASE.jar
file:/C:/Users/Dan/.m2/repository/org/hibernate/hibernate-core/4.3.11.Final/hibernate-core-4.3.11.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/logging/jboss-logging-annotations/1.2.0.Beta1/jboss-logging-annotations-1.2.0.Beta1.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.0.Final/jboss-transaction-api_1.2_spec-1.0.0.Final.jar
file:/C:/Users/Dan/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar
file:/C:/Users/Dan/.m2/repository/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar
file:/C:/Users/Dan/.m2/repository/org/hibernate/common/hibernate-commons-annotations/4.0.5.Final/hibernate-commons-annotations-4.0.5.Final.jar
file:/C:/Users/Dan/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar
file:/C:/Users/Dan/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/jandex/1.1.0.Final/jandex-1.1.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/springframework/spring-aop/4.1.4.RELEASE/spring-aop-4.1.4.RELEASE.jar
file:/C:/Users/Dan/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar
file:/C:/Users/Dan/.m2/repository/net/sf/json-lib/json-lib/2.4/json-lib-2.4-jdk15.jar
file:/C:/Users/Dan/.m2/repository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar
file:/C:/Users/Dan/.m2/repository/net/sf/ezmorph/ezmorph/1.0.6/ezmorph-1.0.6.jar
file:/C:/Users/Dan/.m2/repository/commons-fileupload/commons-fileupload/1.3/commons-fileupload-1.3.jar
file:/C:/Users/Dan/.m2/repository/org/apache/wicket/wicket-util/6.23.0/wicket-util-6.23.0.jar
file:/C:/Users/Dan/.m2/repository/com/mchange/c3p0/0.9.5.2/c3p0-0.9.5.2.jar
file:/C:/Users/Dan/.m2/repository/com/mchange/mchange-commons-java/0.2.11/mchange-commons-java-0.2.11.jar
file:/C:/Users/Dan/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
file:/C:/Users/Dan/.m2/repository/org/apache/myfaces/core/myfaces-impl/2.2.9/myfaces-impl-2.2.9.jar
file:/C:/Users/Dan/.m2/repository/org/apache/myfaces/core/myfaces-api/2.2.9/myfaces-api-2.2.9.jar
file:/C:/Users/Dan/.m2/repository/org/apache/geronimo/specs/geronimo-atinject_1.0_spec/1.0/geronimo-atinject_1.0_spec-1.0.jar
file:/C:/Users/Dan/.m2/repository/commons-digester/commons-digester/1.8/commons-digester-1.8.jar
file:/C:/Users/Dan/.m2/repository/xalan/xalan/2.7.2/xalan-2.7.2.jar
file:/C:/Users/Dan/.m2/repository/xalan/serializer/2.7.2/serializer-2.7.2.jar
file:/C:/Users/Dan/.m2/repository/rome/rome/1.0/rome-1.0.jar
file:/C:/Users/Dan/.m2/repository/jdom/jdom/1.0/jdom-1.0.jar
file:/C:/Users/Dan/.m2/repository/org/python/jython-standalone/2.5.2/jython-standalone-2.5.2.jar
file:/C:/Users/Dan/.m2/repository/rhino/js/1.7R2/js-1.7R2.jar
file:/C:/Users/Dan/.m2/repository/javassist/javassist/3.12.0.GA/javassist-3.12.0.GA.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/weld/weld-core/1.1.33.Final/weld-core-1.1.33.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/weld/weld-api/1.1.Final/weld-api-1.1.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/weld/weld-spi/1.1.Final/weld-spi-1.1.Final.jar
file:/C:/Users/Dan/.m2/repository/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/spec/javax/interceptor/jboss-interceptors-api_1.1_spec/1.0.0.Beta1/jboss-interceptors-api_1.1_spec-1.0.0.Beta1.jar
file:/C:/Users/Dan/.m2/repository/org/slf4j/slf4j-ext/1.7.2/slf4j-ext-1.7.2.jar
file:/C:/Users/Dan/.m2/repository/ch/qos/cal10n/cal10n-api/0.7.7/cal10n-api-0.7.7.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/interceptor/jboss-interceptor-core/2.0.0.Final/jboss-interceptor-core-2.0.0.Final.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/interceptor/jboss-interceptor-spi/2.0.0.Final/jboss-interceptor-spi-2.0.0.Final.jar
file:/C:/Users/Dan/.m2/repository/javax/enterprise/cdi-api/1.0-SP1/cdi-api-1.0-SP1.jar
file:/C:/Users/Dan/.m2/repository/org/jboss/interceptor/jboss-interceptor-api/1.1/jboss-interceptor-api-1.1.jar
file:/C:/Users/Dan/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar
file:/C:/Users/Dan/.m2/repository/javax/interceptor/javax.interceptor-api/3.1/javax.interceptor-api-3.1.jar
file:/C:/Users/Dan/.m2/repository/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar
file:/C:/Users/Dan/.m2/repository/org/slf4j/slf4j-jdk14/1.7.21/slf4j-jdk14-1.7.21.jar
file:/C:/Users/Dan/.m2/repository/org/clojure/clojure/1.8.0/clojure-1.8.0.jar
file:/C:/Users/Dan/.m2/repository/com/vaadin/vaadin-server/7.7.14/vaadin-server-7.7.14.jar
file:/C:/Users/Dan/.m2/repository/com/vaadin/vaadin-sass-compiler/0.9.13/vaadin-sass-compiler-0.9.13.jar
file:/C:/Users/Dan/.m2/repository/org/w3c/css/sac/1.3/sac-1.3.jar
file:/C:/Users/Dan/.m2/repository/com/vaadin/external/flute/flute/1.3.0.gg2/flute-1.3.0.gg2.jar
file:/C:/Users/Dan/.m2/repository/com/vaadin/vaadin-shared/7.7.14/vaadin-shared-7.7.14.jar
file:/C:/Users/Dan/.m2/repository/org/jsoup/jsoup/1.8.3/jsoup-1.8.3.jar
file:/C:/Users/Dan/.m2/repository/org/aspectj/aspectjweaver/1.9.5/aspectjweaver-1.9.5.jar
file:/C:/Users/Dan/.m2/repository/org/apache/click/click-nodeps/2.3.0/click-nodeps-2.3.0.jar
file:/C:/Users/Dan/.m2/repository/ognl/ognl/2.6.9/ognl-2.6.9.jar
file:/C:/Users/Dan/.m2/repository/org/apache/velocity/velocity/1.7/velocity-1.7.jar
file:/C:/Users/Dan/.m2/repository/org/mortbay/jasper/apache-el/8.0.27/apache-el-8.0.27.jar
file:/D:/Program%20Files/JetBrains/IntelliJ%20IDEA%202024.2.3/lib/idea_rt.jar
----------------------------

对比图如下:

image-20241108105240679

发现在AppClassLoader LoadPath这一栏增加了巨量的jar文件,这是怎么回事?

原因是因为IDEA的启动语句

1
"D:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2024.2.3\lib\idea_rt.jar=1928:D:\Program Files\JetBrains\IntelliJ IDEA 2024.2.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;......#超级多" com.study.java.MainTest

可以看到,第一个参数是确定启动的java程序全路径,第二个-javaagent参数暂时不做学习

-Dfile.encoding=UTF-8是做了编码统一,最后就是-classpath指定ClassPath 这个参数后面跟了很多包路径

所以最后,IDEA只是根据我们的设置去自动配置好了需要执行的命令,不需要我们去手动配置相关环境

豪用,爱用

Tips

  • 自JDK 1.2以后,JVM采用了委托(delegate)模式来载入class。采用这种设计的原因可以参考: http://java.sun.com/docs/books/tutorial/ext/basics/load.html
  • ExtClassLoader 和 AppClassLoader在JVM启动后,会在JVM中保存一份,并且在程序运行中无法改变其搜索路径。如果想在运行时从其他搜索路径加载类,就要产生新的类加载器。

参考

https://blog.csdn.net/sayyy/article/details/81120749 !!

Dan❤Anan
Built with Hugo
主题 StackJimmy 设计