我从 native 代码启动 JVM,然后启动 Jetty。然后,Web 应用程序加载一个名为 JPeripheral 的库。 native 启动器和 JPeripheral 都依赖于名为 Jace 的 native 库。当 Web 应用程序尝试加载 Jace Java 时,会抛出:
java.lang.UnsatisfiedLinkError: native 库 jace.dll 已在另一个类加载器中加载
这是 web 应用程序内的 ClassLoader 层次结构:
WebAppClassLoader -> sun.misc.Launcher$AppClassLoader -> sun.misc.Launcher$ExtClassLoader
Jace.dll 和 Jetty 均由 sun.misc.Launcher$AppClassLoader
加载(由 native 启动器使用)。
JPeripheral由WebAppClassLoader加载。
解决此问题的一种方法是从 sun.misc.Launcher$AppClassLoader
加载 JPeripheral(因此 jace.dll 从同一个类加载器加载两次)。我该怎么做?
最佳答案
事实证明,您可以使用 WebAppContext.addSystemClass() 强制 WebAppClassLoader 从父 ClassLoader 加载 JPeripheral。就我而言,WebAppContext.addSystemClass("org.jperipheral.")
成功了。
关于java - 强制 Jetty 加载父 ClassLoader 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8011643/