Java 在WildFly/JBoss中设置TCCL
我遇到了所讨论的问题,该问题导致Java 在WildFly/JBoss中设置TCCL,java,jboss,classloader,classnotfoundexception,wildfly,Java,Jboss,Classloader,Classnotfoundexception,Wildfly,我遇到了所讨论的问题,该问题导致java.lang.ClassNotFoundException:\uuu重定向/\uu数据类型工厂错误 在上面的线程中,Jason Greene说“…确保将TCCL设置为指向模块类加载器(并在finally块中将其重置回原始状态)”。我想尝试一下,但不知道如何设置TCCL或获取模块类加载器如何将TCCL设置为模块类加载器? 其他信息: 我在使用JAXB编组XML时遇到错误 我正在使用WildFly 8.0.0.CR1 答案出人意料地简单(对于我的上下文!)我有
java.lang.ClassNotFoundException:\uuu重定向/\uu数据类型工厂
错误
在上面的线程中,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和_重定向/_数据类型工厂问题的类
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);
}
}
}
重置类加载器需要在finally块中完成。Good call@JamesR.Perkins。根据您的建议进行更新。是否必须始终还原类加载器,或者仅当您的交换入类加载器没有完成原始类加载器所做的一切时才需要还原类加载器?是的,即使TCCL为空,也必须始终还原TCCL。您永远不知道其他组件/依赖项将尝试做同样的事情。