I am using the Nashorn JavaScript Engine in Java 11 which works fine except it will be deprecated soon. I have tried GraalVM which I find quite worse as it takes 13-14 seconds to execute a simple expression (e.g. 2+3). Is there any other alternative that I could use or should I try GraalVM with some other approach (if there is any)?
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
engine.eval("2+3");
The Nashorn engine has been deprecated in JDK 11 as part of JEP 335 and and has been removed from JDK15 as part of JEP 372. GraalVM can step in as a replacement for JavaScript code previously executed on the Nashorn engine. GraalVM provides all the features for JavaScript previously provided by Nashorn.
The deprecation-for-removal of Nashorn in JDK 11 was confirmed in June 2018, causing the proposed removal to be flagged at every use of the jdk.
With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards. Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.
Nashorn engine is an open source implementation of the ECMAScript Edition 5.1 Language Specification.
If someone still finds this question relevant, a viable, still actively maintained alternative to Nashorn/Rhino script engines is the J2V8 binding for the JVM.
Artifacts can be found here (be sure to use this link to get updated releases)
Multi-platform support is builtin via JNI
Though its mechanics are a bit different than those of the ScriptEngine
API, performance is (as expected) better. and you don't have to deal with weird side effects like unintended class-loading caused by misuse of the API.
I have successfully used it myself in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With