JarClassLoader - loading jars dynamically - how to?

梦想与她 提交于 2020-02-04 06:00:47

问题


I have seen many class loader questions, but still was not able to figure why, the error here.

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.

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();
     }
  }
}

Method called is:

 public void callthis() {
    DFSUtil = new DFSAccessAPIImpl();
    .........
 }

This class instantiation internally uses hadoop modified jar, which is not picked up from my classloader and it throws ClassNotFoundException.

What is that I am doing wrong ?

JarClassLoader used here is jcloader : org.xeustechnologies.jcl.JarClassLoader


回答1:


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/cz/sohlich/app/service/impl/PluginHolderImpl.java



来源:https://stackoverflow.com/questions/31532577/jarclassloader-loading-jars-dynamically-how-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!