本文介绍了导入冲突-由于包结构相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码对abc.jar文件有依赖性.该abc.jar文件在包org.apache.log4j下具有一个名为Logger的类,但这不是来自Apache的Logger.

My code has a dependency on abc.jar file. This abc.jar file has a class called Logger, under the package org.apache.log4j, but this is not the Logger from Apache.

我想使用Apache的Logger.我在maven中为Apache的Logger添加了一个依赖项.但是问题是,当我想使用Apache的Logger时,它会自动从abc.jar文件中选择Logger.

I want to use Logger from Apache. I have added a dependency in maven for Apache's Logger. But the problem is that when I want to use Apache's Logger, it automatically picks up the Logger from abc.jar file.

推荐答案

当两个JAR具有相同的Package且具有相同的类名时,从特定的JAR加载类.

To load class from specific JAR when both JAR has same Package with same class name.

1)您需要指定使用类的JAR路径.

1) You need to specify the path of the JAR from which you need to use the class.

为此,您可以使用

URL myURL = new URL("jar:file:" +OfficialApacheJarPath+"!/");
    URL[] urls =  new URL[]{myURL};
    URLClassLoader cl = URLClassLoader.newInstance(urls);
    Class c = cl.loadClass("Logger");

这篇关于导入冲突-由于包结构相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:55