Java - 如何加载同一个类的不同版本?

kms*_*333 36 java jvm class classloader

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

我在jars 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]
Run Code Online (Sandbox Code Playgroud)

而在另一个班级:

Classloader myClassLoader = [magic that includes v2.jar and ignores v1.jar]
Hello hello = myclassLoader.load[com.abc.Hello]
Run Code Online (Sandbox Code Playgroud)

我想避免使用OSGi.

hel*_*ios 43

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

通常的情况是使用父类加载器中存在的类.因此,如果你想要两个版本,那些类不能存在.

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

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();
Run Code Online (Sandbox Code Playgroud)

  • 因为关于NOT在主类路径中没有两个版本的解释而被推翻.如果您还可以解释他们如何在代码中使用c1和c2以及如何编译它将会很有帮助.也就是说,他们如何调用`c1.someMethod()`? (3认同)
  • @helios我已经实现了你的提议来处理elasticsearch客户端使用的非向后兼容版本.Checkout https://github.com/atulsm/ElasticsearchClassLoader (2认同)

Ale*_*exR 7

你几乎写了解决方案.我希望以下代码片段能够提供帮助.

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

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