我遇到了讨论的here问题,该问题导致了java.lang.ClassNotFoundException: __redirected/__DatatypeFactory
错误。
在上面的线程中,Jason Greene说:“ ...确保将TCCL设置为指向模块类加载器(并在finally块中将其重置为原始)。”我想尝试一下,但无法弄清楚如何设置TCCL或获取模块类加载器。如何将TCCL设置为模块类加载器?
附加信息:
使用JAXB封送XML时遇到错误
我正在使用WildFly 8.0.0.CR1
最佳答案
答案最终变得非常简单(就我的上下文而言!),我有一个带有正确TCCL上下文的类。
这将是初始化时具有正确加载程序的类
public class GoodClass {
private ClassLoader goldenLoader;
public GoodClass() {
// the class loader must be the 'correct' one at this point. We save the 'correct' one for later
this.goldenLoader = Thread.currentThread().getContextClassLoader();
}
public static ClassLoader getGoodClassLoader() {
return goldenLoader;
}
}
这将是具有TCCL和__redirected / __ DatatypeFactory问题的类
public class BadClass {
public void myMethod() {
ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
try {
// you can NOT do stuff that relies on the set TCCL here
ClassLoader goldenLoader = GoodClass.getGoodClassClassLoader().getGoldenLoader();
Thread.currentThread().setContextClassLoader(goldenLoader);
// you CAN do stuff that relies on the set TCCL here
} finally {
// use this if you need to restore the orignal loader
Thread.currentThread().setContextClassLoader(origLoader);
}
}
}