Issues with Nashorn Script Engine and JDK17 in Confluence 8.8.0

Hi everyone,

Confluence 8.8.0 recommends using JDK17. As part of our implementation, we rely on the ScriptEngineManager to execute JavaScript code. However, JDK17 no longer includes the Nashorn script engine by default, which has led us to explicitly add it back via our pom.xml as follows:

<dependency>
    <groupId>org.openjdk.nashorn</groupId>
    <artifactId>nashorn-core</artifactId>
    <version>15.3</version>
    <scope>runtime</scope>
</dependency>

Despite this, when attempting to retrieve the Nashorn script engine with:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

“engine” becomes null, indicating that the script engine is not being found.

Has anyone encountered this issue and found a workaround?

Ideas

  • Try removing the “runtime” scope
  • Maybe it is an OSGi class loader issue and you can’t use ScriptEngineManager → Invoke the engine direcly

Thank you for the advice.
I’ve tried both suggestions, but I still couldn’t resolve the issue.

 ScriptEngineFactory factory = new NashornScriptEngineFactory();
 ScriptEngine engine = factory.getScriptEngine();

Caused by: org.osgi.framework.BundleException: Unable to resolve jp.kenputer.calc-macro [317](R 317.0): missing requirement [jp.kenputer.calc-macro [317](R 317.0)] osgi.wiring.package; (osgi.wiring.package=jdk.nashorn.api.scripting) Unresolved requirements: [[jp.kenputer.calc-macro [317](R 317.0)] osgi.wiring.package; (osgi.wiring.package=jdk.nashorn.api.scripting)]

        <dependency>
            <groupId>org.openjdk.nashorn</groupId>
            <artifactId>nashorn-core</artifactId>
            <version>15.4</version>
        </dependency>

                     <instructions>
                        <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
                        <Export-Package>jp.kenputer.qcalcmacro.api</Export-Package>
                        <Import-Package>
                            org.springframework.osgi.*;resolution:="optional",
                            jdk.nashorn.api.scripting,
                            *
                        </Import-Package>
                        <Spring-Context>*</Spring-Context>
                    </instructions>

You should remove the Import-Package, which also resolves to the old package (I assume)…

When you use the “new” Nashorn as compile dependency, it will be directly copied into your final jar.

Please note: Nashorn is GPL2, which might be a problem for you… Depending on the scripts you want to evaluate → maybe use something else, simpler for this :wink: