To load two jar packages with identical package names and class names

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.