44

我已经阅读了很多关于 Java 类加载器的内容,但到目前为止,我还没有找到这个简单问题的答案:

我在罐子v1.jarv2.jar中有两个版本的com.abc.Hello.class。我想在我的应用程序中使用两者。这样做的最简单方法是什么?

我不希望会那么简单,但是这些方面的东西会很棒:

Classloader myClassLoader = [magic that includes v1.jar and ignores v2.jar]
Hello hello = myclassLoader.load[com.abc.Hello]

在不同的班级:

Classloader myClassLoader = [magic that includes v2.jar and ignores v1.jar]
Hello hello = myclassLoader.load[com.abc.Hello]

我想避免使用 OSGi。

4

4 回答 4

52

你走对了。你必须考虑一些事情。

正常情况是使用存在于父类加载器中的类。因此,如果您想要两个版本,则这些类必须不存在。

但是如果你想进行交互,你可以使用反射,甚至更好的通用接口。所以我会这样做:

common.jar:
BaseInterface

v1.jar:
SomeImplementation implements BaseInterface

v2.jar:
OtherImplementation implements BaseInterface

command-line:
java -classpath common.jar YourMainClass
// you don't put v1 nor v2 into the parent classloader classpath

Then in your program:

loader1 = new URLClassLoader(new URL[] {new File("v1.jar").toURL()}, Thread.currentThread().getContextClassLoader());
loader2 = new URLClassLoader(new URL[] {new File("v2.jar").toURL()}, Thread.currentThread().getContextClassLoader());

Class<?> c1 = loader1.loadClass("com.abc.Hello");
Class<?> c2 = loader2.loadClass("com.abc.Hello");

BaseInterface i1 = (BaseInterface) c1.newInstance();
BaseInterface i2 = (BaseInterface) c2.newInstance();
于 2012-08-01T12:47:42.243 回答
9

您几乎已经编写了解决方案。我希望下面的代码片段会有所帮助。

ClassLoader cl = new URLClassLoader(new URL[] {new File("v1.jar").toURL()}, Thread.currentThread().getContextClassLoader());
Class<?> clazz = cl.loadClass("Hello");

替换v1.jarv2.jar,此代码将加载版本 #2。

于 2012-08-01T12:43:39.160 回答
1

对于@helios checkout github.com/atulsm/ElasticsearchClassLoader接受的答案的示例实现

于 2016-02-13T19:26:53.647 回答
0

这取决于你要对这两个版本做什么以及如何处理,但一般来说,你至少可以在不同的类加载器中加载 2 个版本的类,然后设置Thread.contextClassloader()并播放......

请参阅http://www.javaworld.com/javaqa/2003-06/01-qa-0606-load.htmlhttp://docs.oracle.com/javase/jndi/tutorial/beyond/misc/classloader.html

于 2012-08-01T12:43:56.923 回答