kms*_*333 36 java jvm class classloader
我已经阅读了很多关于Java类加载器的内容,但到目前为止我还没有找到这个简单问题的答案:
我在jars v1.jar和v2.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)
你几乎写了解决方案.我希望以下代码片段能够提供帮助.
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.jar为v2.jar,此代码将加载版本#2.
| 归档时间: |
|
| 查看次数: |
24204 次 |
| 最近记录: |