首先从表现层介绍,后续后深入原理。
1,先简单介绍maven如何生成jar文件方便测试
配置了一个manifest标签来配置Main函数的入口。然后通过如下指令来实现打包。mvn assembly:assembly maven-assembly-plugin 2.4 jar-with-dependencies Main.Main make-assembly package single
2.自定义两个jar包,其中包含相同包名和类名
与export的导入顺序有关。只会加载第一个,并且运行正常。
3.自定义jar和jdk包, 其中包含相同的包名和类名
与export的导入顺序有关。同样是只会加载第一个,但是如果加载自定义的jar运行会报错。加载 jdk正常。
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