JarClassLoader-动态加载jar-如何? [英] JarClassLoader - loading jars dynamically - how to?

查看:276
本文介绍了JarClassLoader-动态加载jar-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多类加载器问题,但是仍然无法弄清楚为什么,这里的错误。

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.

https://bitbucket.org/rsohlich/plagdetector/src/432b52f252ff7647221b7e91b08731bd9cbe2a70/PlagDetectorSpring/src/main/java /sohlich/app/service/impl/PluginHolderImpl.java

这篇关于JarClassLoader-动态加载jar-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆