Skip to content

Using Nashorn with different Java versions

Attila Szegedi edited this page Feb 13, 2021 · 2 revisions

Nashorn has two distribution channels:

  • it ships as a built-in component of OpenJDK-based Java distributions between Java versions 8 to 14. It no longer ships starting with Java 15 and is not actively developed.
  • it is available as a standalone library distributed on Maven Central. The standalone library is usable with Java 11 and above and is actively developed.

We'll refer to these as "built-in" and "standalone" versions of Nashorn. The table below summarizes what versions of Java can use what version of Nashorn:

Java Built-in Standalone
7 and earlier
8-10
11-14
15 and later

Using Nashorn on Java 11 to 14

Some consideration is necessary if you want to distribute an application that uses Nashorn and it should run both on Java 11-14 and Java 15+ versions. You can bundle standalone Nashorn with your application, but note that when running on Java versions between 11-14, built-in Nashorn will also be available to it. Which one gets used depends on several factors:

  • If you create your script engine using Nashorn APIs, you can unambiguously select which one you want by addressing the relevant package;
    • you can create an engine factory for the built-in version by instantiating jdk.nashorn.api.scripting.NashornScriptEngineFactory and
    • an engine factory for the standalone version by instantiating org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory. If you use explicit APIs, we recommend you use standalone Nashorn in this case as your application is then future-proof for Java 15 and beyond and leave built-in Nashorn inactive.
  • If you create your script engine using javax.script.* APIs, i.e. with an expression like ScriptEngineManager.getEngineByName("nashorn") then you will get:
    • standalone Nashorn, if you loaded Nashorn as a module through the --module-path option as it gets precedence there, but
    • built-in Nashorn, if standalone Nashorn is loaded on the ordinary classpath.
Clone this wiki locally