Search code examples
javascriptjavaosgiapache-karafscriptengine

ScriptEngineManager and ScriptEngine can not be loaded in Karaf OSGi (Nashorn not found)


I am trying to use ScriptEngineManager and ScriptEngine to execute some JavaScript code with Java. I use Java 8 I am executing this code under Karaf OSGi.

The example I used works fine in a sample Java Class, but packing it in a bundle gives me this error ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not found

When I look to the headers of the bundle, I see that the bundle imports javax.script, the package of the called classes but it seems that (nashorn) the provider is not found.

Thanks


Solution

  • Go to file etc/config.properties (located inside of Karaf installation), find configuration property org.osgi.framework.bootdelegation and add jdk.nashorn.* to it. Complete configuration will be like this:

    org.osgi.framework.bootdelegation=org.apache.karaf.jaas.boot,org.apache.karaf.jaas.boot.principal,sun.*,com.sun.*,javax.transaction,javax.transaction.*,javax.xml.crypto,javax.xml.crypto.*,org.apache.xerces.jaxp.datatype,org.apache.xerces.stax,org.apache.xerces.parsers,org.apache.xerces.jaxp,org.apache.xerces.jaxp.validation,org.apache.xerces.dom,sun.*,jdk.nashorn.*
    

    Bug is reported to Karaf community.

    Edit: If you use Pax Exam Container 4.x for Karaf in your tests then don't forget to add org.ops4j.pax.exam.CoreOptions.bootDelegationPackage("jdk.nashorn.*") to the configuration of your test container.