Before updating from IBM Java 7 to 8 the following code used to work in an Equinox OSGi environment:
ScriptEngineManager manager = new ScriptEngineManager(getClass().getClassLoader());
ScriptEngine engine = manager.getScriptEngineManager().getEngineByExtension("js");
engine.eval("<... js code ...>");
((Invocable) engine).getInterface(MyInterface.class) // ECMAScript exception
In the previous setup I used Rhino as engine which is now not registered as available ScriptEngineFactory anymore but still on the classpath, only "Oracle Nashorn".
After updating to IBM Java 8 (including Nashorn) I get the following exception:
ECMAScript Exception: TypeError: Can not find a common class loader for ScriptObject and MyInterface.
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:69)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:225)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:197)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:184)
at jdk.nashorn.internal.runtime.linker.AdaptationResult.typeError(AdaptationResult.java:82)
at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory$AdapterInfo.getAdapterClass(JavaAdapterFactory.java:326)
at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getAdapterClassFor(JavaAdapterFactory.java:162)
at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getAdapterClassFor(JavaAdapterFactory.java:148)
at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getConstructor(JavaAdapterFactory.java:202)
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterfaceInner(NashornScriptEngine.java:291)
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterface(NashornScriptEngine.java:216)
...
Nashorn tries to check if the classloader of jdk.nashorn.internal.runtime.ScriptFunction (Ext ClassLoader) can load the given interface (Bundle ClassLoader) or vice versa via:
Class.forName(classBName, false, clazzA.getLoader()) == classB;
Any ideas how to configure OSGi to let one of the classloaders see the other or other workarounds for working with Nashorn on Java 8 + OSGi?