Forcing Jetty to load classes in the parent ClassL

2019-06-08 11:55发布

问题:

I launch a JVM from native code, then launch Jetty. A webapp then loads a library called JPeripheral. Both the native launcher and JPeripheral depend a native library called Jace. When the webapp tries loading Jace Java throws:

java.lang.UnsatisfiedLinkError: Native Library jace.dll already loaded in another classloader

Here is the ClassLoader hierarchy inside the webapp:

WebAppClassLoader -> sun.misc.Launcher$AppClassLoader -> sun.misc.Launcher$ExtClassLoader

Jace.dll and Jetty are both loaded by sun.misc.Launcher$AppClassLoader (used by the native launcher). JPeripheral is loaded by WebAppClassLoader.

One way to solve this problem would be to load JPeripheral from sun.misc.Launcher$AppClassLoader (so jace.dll gets loaded twice from the same Classloader). How do I do that?

回答1:

It turns out that you can force WebAppClassLoader to load JPeripheral from the parent ClassLoader using WebAppContext.addSystemClass(). In my case WebAppContext.addSystemClass("org.jperipheral.") did the trick.