• 有关两个jar包中包含完全相同的包名和类名的加载问题


    首先从表现层介绍,后续后深入原理。

    1,先简单介绍maven如何生成jar文件方便测试

        

    <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
    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

     
  • 相关阅读:
    石家庄地铁线路查询系统(补)
    构建之法阅读笔记03
    构建之法阅读笔记02
    Day 3-3 内置方法
    Day3-2 函数之递归
    Day3-1 函数
    Day2 列表,元组,字典,集合
    Day1 基础知识
    Day1. Python基础知识
    iptables防火墙配置
  • 原文地址:https://www.cnblogs.com/qunan/p/7473482.html
Copyright © 2020-2023  润新知