首先从表现层介绍,后续后深入原理。
1、先简单介绍maven如何生成jar文件方便测试
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
|
< plugin >
< artifactId >maven-assembly-plugin</ artifactId >
< version >2.4</ version >
< configuration >
< descriptorRefs >
< descriptorRef >jar-with-dependencies</ descriptorRef >
</ descriptorRefs >
< archive >
< manifest >
< mainClass >Main.Main</ mainClass >
</ manifest >
</ archive >
</ configuration >
< executions >
< execution >
< id >make-assembly</ id >
< phase >package</ phase >
< goals >
< goal >single</ goal >
</ goals >
</ execution >
</ executions >
</ plugin >
配置了一个manifest标签来配置Main函数的入口。然后通过如下指令来实现打包。
mvn assembly:assembly
|
与export的导入顺序有关。只会加载第一个,并且运行正常。
3、自定义jar和jdk包, 其中包含相同的包名和类名
与export的导入顺序有关。同样是只会加载第一个,但是如果加载自定义的jar运行会报错。加载 jdk正常。
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
|
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
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 thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
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;
}
}
|
4、mvn jar包冲突常用命令
mvn dependency:analyze,mvn dependency:tree
以上这篇浅谈两个jar包中包含完全相同的包名和类名的加载问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/qunan/archive/2017/09/04/7473482.html