Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nashorn alternative for Java 11 [closed]

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");
like image 535
Sohaib Zafar Avatar asked Oct 01 '19 07:10

Sohaib Zafar


People also ask

What is replacing Nashorn?

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.

When was Nashorn removed Java?

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.

Why is Nashorn deprecated?

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.

Is Nashorn open source?

Nashorn engine is an open source implementation of the ECMAScript Edition 5.1 Language Specification.


1 Answers

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.

like image 97
Sheinbergon Avatar answered Oct 06 '22 00:10

Sheinbergon