Load my own Xalan implementation where an older Xalan is loaded to the parent classloader

别等时光非礼了梦想. 提交于 2020-01-22 02:24:07

问题


I'm writing a plugin for a framework that loads my code as a child classloader

The problem is that that framework uses a certain Xerces version, that is not compatible with my code, I want to use my "own" jar for xerces, but it seems since the old one was already loaded, I can't seem to make my code use mine.

I'm looking for some classloader seperation, I know it's a know problem, but can't seem to solve it

Is there any framework, library, or code sample to user locally a newer jar in such a scenario?


回答1:


Have you tried loading the classes of your framework and Xerces libs as a part of the ExtClassLoader by placing them in path corresponding to the java.ext.dirs system property? This way the framework's version of Xerces implementation will be loaded by the ExtClassLoader.

You can then place your version of Xerces implementation in the path corresponding to the java.class.path system property to be loaded by the AppClassLoader.

I have not tried this myself, but considering the class loading hierarchy this should work. You can learn more about the class loading hierarchy here - http://onjava.com/pub/a/onjava/2005/01/26/classloading.html




回答2:


try doing:

ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(childClassLoader);
try{
    // do xml parsing
}finally{
    Thread.currentThread().setContextClassLoader(oldContextClassLoader);
}


来源:https://stackoverflow.com/questions/5440395/load-my-own-xalan-implementation-where-an-older-xalan-is-loaded-to-the-parent-cl

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