To load two jar packages with identical package names and class names
First, we will introduce it from the presentation layer, and then go deep into the principles.
1. First, let's briefly introduce how maven generates jar files for 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> </e Xecutions> </plugin> configures a manifest tag to configure the Main function entry. Then, use the following command to package. Mvn assembly: assembly
2. Customize two jar packages, including the same package name and Class Name
It depends on the import sequence of the export. Only the first one is loaded and runs normally.
3. Custom jar and jdk packages, including the same package name and Class Name
It depends on the import sequence of the export. Similarly, only the first one will be loaded, but an error will be reported if the custom jar is loaded. Jdk loading 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. Common commands for mvn jar package conflicts
Mvn dependency: analyze, mvn dependency: tree