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.
source share