我正在尝试将复杂的应用程序(jForexAPi、DDS2)放入 OSGi bundle 中。我制作了两个带有嵌入式依赖项的包,包括编译时和运行时(传递)。所以我在 .jar 包中包含带有 .class-es 的包。
当我尝试使用时,我得到了 ClassNotFoundException,因为 DDS2 实现在运行时通过其线程的类加载器加载类。不知怎的,像这样:
Class e = Thread.currentThread().getContextClassLoader().loadClass("com.dukascopy.charts.main.DDSChartsControllerImpl");
我有两个问题:
- 如何确定 karaf 中线程的父包?
- 如何解决 OSGi 中的运行时类加载等问题?有没有办法允许或发现运行时类加载?
最佳答案
How can I determine the parent bundle of a thread in karaf?
你不能。线程没有父包。如果您指的是线程上下文类加载器,那么 OSGi 中根本没有定义它。 TCC 通常是 Java EE 世界中 Web 应用程序的类加载器。然而,在 OSGi 中,它甚至可以为 null 或任何其他值。永远不要使用它。
How can I solve issues like runtime classloads in OSGi? Is there a way to allow or discover runtime classloads?
你可以做两件事:
- 为项目做出贡献,以允许配置将用于加载这些类的类加载器
- 实现解决方法:分析 ClassNotFoundException 的堆栈跟踪并找到可以设置线程上下文类加载器的位置
如果您选择第二个选项,您的代码将类似于以下内容:
Thread currentThread = Thread.currentThread();
ClassLoader previousCL = currentThread.getContextClassLoader();
try {
currentThread.setContextClassLoader(DDSChartsControllerImpl.class.getClassLoader());
callNextFunctionOnStacktrace();
} finally {
// You should set the original CL back as other technology might use the TCC tricks, too
currentThread.setContextClassLoader(previousCL);
}
关于java - 如果 OSGi 包使用类加载器,我如何检测它的运行时库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756767/