I need my software which uses the Nashorn JavaScript engine to be compatible with Java 1.8 upwards.
If I use the one bundled with the JDK it will not work on Java >= 15. If I use the standalone Nashorn it will not be compatible with Java < 15.
Is there a way to keep compatibility? I have already thought about reflection but there might be more efficient ways.
-
Why is it not compatible? Why can't you just not distribute it for Java <15? – OrangeDog Jan 30 '21 at 14:08
-
@OrangeDog The standalone Nashorn is compiled with Java 15 while my project uses Java 8. Keeping Java 15 out of distribution would be an _in extremis_ option, I would rather prefer a workaround. – iamgio Jan 30 '21 at 14:12
3 Answers
Nashorn maintainer here. It should be possible to compile standalone Nashorn from sources for Java 11. I'm pretty sure we can't go below that, unfortunately. Are you using its API directly? In that case you unfortunately have a mismatch between built-in Nashorn's package names and the standalone version. If you can only rely on javax.script.*
API then it should be possible to use both.
I want to understand your use case a bit better and help you arrive at a solution.

- 4,405
- 26
- 24
-
Thank you for the answer. I access the API directly only to call `ScriptUtils#convert`. You can see it yourself [here](https://github.com/iAmGio/chorus/tree/master/src/main/java/org/chorusmc/chorus/addon) ([here](https://github.com/iAmGio/chorus/blob/master/src/main/java/org/chorusmc/chorus/addon/Addon.kt#L57) is the call) (I also access `NashornScriptEngineFactory` directly but I should switch to the standard engine initialization). – iamgio Feb 02 '21 at 09:24
-
Hi, I noted on your other post https://stackoverflow.com/a/66190018/131552 that starting with Nashorn 15.2, you can now use it with Java 11 and above so you can have a single application that works with Nashorn for Java 11 and later, no need to switch at Java 15. However, Java 11 is as far in the past as it's reasonably possible to go. – Attila Szegedi Feb 13 '21 at 21:22
As you have noticed, Nashorn was deprecated in JEP 335 (JDK Enhancement Proposal) in Java 11 and it has subsequently been removed in JEP 372 that was delivered as part of Java 15. The motivation for the removal is
With the rapid pace at which ECMAScript language constructs, along with APIs, are adapted and modified, we have found Nashorn challenging to maintain.
Consequently, it will not be an option in the future. Instead I suggest that you take a look at options like GraalVM if you are looking for alternatives. As stated in the introduction:
It is designed for applications written in Java, JavaScript, LLVM-based languages such as C and C++, and other dynamic languages. It removes the isolation between programming languages and enables interoperability in a shared runtime.

- 32,104
- 16
- 121
- 156
-
Thank you for the answer. I have already tried GraalVM via Maven, it runs smoothly but the dependencies taking up 40MB make the software a bit tricky to be distributed without an installer. – iamgio Jan 30 '21 at 14:29
If you cannot simply not distribute a standalone Nashorn JAR for JDKs <15, you should be able to build a multi-release version of it instead.
The root level should provide nothing, and then have all the actual classes in META-INF/versions/15/
Documentation: JAR Specification: Multi-release JAR files

- 36,653
- 12
- 122
- 207