Can I run ECMAScript 6 from the Java 9 Nashorn engine

Java 9 now includes support for ECMAScript 6, as stated in this article . However, he does not explain how to run it from Java using ScriptEngine . The Java related journal also does not explain this. The article says the following:

To activate ES6 support, use --language=es6 on the command line.

This works with jjs , but I cannot find a way to include this from Java code. To test it, I used the following code:

 import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; ... ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("application/javascript"); try { engine.eval("const a = 20;"); } catch (ScriptException e) { e.printStackTrace(); } 

The following exception failed:

 javax.script.ScriptException: <eval>:1:0 Expected an operand but found const const a = 20; ^ in <eval> at line number 1 at column number 0 [STACK TRACE OMITTED] 

I tried to list all available ScriptEngineFactories with this code:

 import java.util.List; import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; ... List<ScriptEngineFactory> factories = new ScriptEngineManager().getEngineFactories(); for (ScriptEngineFactory factory : factories) { System.out.println("-----------------------"); System.out.println(factory.getLanguageName()); System.out.println(factory.getLanguageVersion()); } 

Only output the following:

 ----------------------- ECMAScript ECMA - 262 Edition 5.1 

Does this mean that I cannot run ECMAScript 6 with Java and use only jjs ? Or something I missed?

Thanks in advance.

+5
source share
1 answer

While looking at Nashorn's questions here, I came across this question . His answers describe two ways to pass command-line arguments to the Nashorn engine. This answer suggests using NashornScriptEngineFactory , the following code:

 import javax.script.ScriptEngine; import javax.script.ScriptException; import jdk.nashorn.api.scripting.NashornScriptEngineFactory; ... ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine("--language=es6"); try { engine.eval("const a = 20;\n" + "print(a);"); } catch (ScriptException e) { e.printStackTrace(); } 

Although this works, this is not a good solution because it allows you to use the jdk package, which is not an officially supported package. Another answer says you can set arguments with the nashorn.args system property. Code:

 import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; ... System.setProperty("nashorn.args", "--language=es6"); ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn"); try { engine.eval("const a = 20;\n" + "print(a);"); } catch (ScriptException e) { e.printStackTrace(); } 

This is also not a good way to do this because it relies on:

  • The existing Nashorn mechanism, which might not be the case in other Java distributions, and
  • Nashorn engine updated to support language command line option.

I personally prefer the first version, because it will throw a ClassNotFoundException in Java 1.8, since NashornScriptEngineFactory does not exist there, and the second version just silently ignores the property setting.

+2
source

Source: https://habr.com/ru/post/1275485/


All Articles