JarClassLoader-动态加载jar-如何? [英] JarClassLoader - loading jars dynamically - how to?
问题描述
我已经看到很多类加载器问题,但是仍然无法弄清楚为什么,这里的错误。
I have seen many class loader questions, but still was not able to figure why, the error here.
我正在编写一个使用2个版本的jars的程序。需要一个从旧存储中获取内容,另一个需要将内容存储在新存储中。
I am writing a program which uses 2 versions of jars. One is needed to get content from older storage, and another to store content in new storage.
由于我需要一次加载两个罐子,所以使用JarClassLoader创建用于添加一个jar并加载其类的代理。但是我面临ClassNotFoundException。
Since, I need either of the jar to be loaded at a time, I used JarClassLoader to create a proxy for adding one jar and loading its classes. But I face ClassNotFoundException.
public class HbaseMigrator implements Runnable {
public void run() {
JarClassLoader jcl = new JarClassLoader();
jcl.add("hadoop-0.13.0-core-modified-1.jar");
Object obj1 = JclObjectFactory.getInstance().create(jcl, "UserMigThreadImpl", toProcessQueue,threadName, latch,DBUtil,lock);
MigThread mig = JclUtils.cast(obj1, MigThread.class, jcl);
Thread.currentThread().setContextClassLoader(jcl);
try {
Method method = MigThread.class.getMethod("callthis", new Class[]{});
method.invoke(mig, new Object[]{});
// mig.callthis();
} catch( Exception e) {
e.printStackTrace();
} catch(Error er) {
er.printStackTrace();
}
}
}
调用的方法是:
public void callthis() {
DFSUtil = new DFSAccessAPIImpl();
.........
}
此类的实例化内部使用的是 hadoop 修改过的jar,它不是从我的类加载器中提取的,它会抛出 ClassNotFoundException
。
This class instantiation internally uses hadoop modified jar, which is not picked up from my classloader and it throws ClassNotFoundException
.
我做错了什么?
这里使用的JarClassLoader是jcloader:
org.xeustechnologies.jcl。 JarClassLoader
JarClassLoader used here is jcloader : org.xeustechnologies.jcl.JarClassLoader
推荐答案
我在将插件加载到应用程序时遇到了此问题,因此我决定尝试从路径中的所有jar加载所有.class文件。
I experienced this problem with loading plugins to my application, so I decided to try to load all .class files from all jars in path. Maybe this code snipped from my app will help you.
这篇关于JarClassLoader-动态加载jar-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!