Reputation: 93
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.
Upvotes: 2
Views: 2106
Reputation: 4595
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.
Upvotes: 4
Reputation: 38807
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
Upvotes: 0
Reputation: 33779
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.
Upvotes: 1