The following editor will bring you a brief discussion on the loading problem of two jar packages containing exactly the same package name and class name. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.
First we will introduce it from the presentation layer, and then we will delve into the principles.
1. First, briefly introduce how maven generates jar files to facilitate testing
<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. Customize two jar packages, which contain the same package name and class name.
is related to the import order of export. Only the first one will load and run fine.
3. Custom jar and jdk packages, which contain the same package name and class name
is related to the import order of export. Similarly, only the first one will be loaded, but if you load a custom jar and run it, an error will be reported. Loading jdk is normal.
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. Commonly used commands for mvn jar package conflicts
mvn dependency:analyze,mvn dependency: tree
The above is the detailed content of Loading problem when two jar packages contain exactly the same package name and class name. For more information, please follow other related articles on the PHP Chinese website!