Different jar inside the same package name Class name How to distinguish the import

Source: Internet
Author: User

Today in the project to meet a very interesting problem, tortured me for a long time, but eventually resolved, leave this article to commemorate a moment.

problems encountered: the same piece of code, in the colleague that is, I can not find a method. The quoted package is also the same, this problem is the first time to encounter, how to do? Look for the reason, look at the referenced package, only to find that the package name I refer to is the same as the package name referenced by the colleague, but the difference is that the package I referred to is not a jar. This means that different jars contain the same package name and class name. The cause of the problem has been found, how to solve it? How do you specify the package in the jar you want to call yourself?

To solve this problem, you need to understand how the JVM loads. When the JVM loads the package, it looks from the classpath path from top to bottom, finds the first one and then references it.

Package name class names are the same, the JVM can not distinguish between, the general IDE will prompt a conflict and error, if not error, then only the first package is introduced (in the Classpath path in front of the package), the second package will be ClassLoader load class when the judgment is repeated and ignored.

Knowing the way the JVM loads the package, it's easy to solve the problem by putting the jar that needs to refer to the package in front of you, that is, on top of the classpath path.

Different jar inside the same package name Class name How to distinguish the import

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.