Java 9 now includes ECMAScript 6 support, as claimed by this article. However, it doesn't explain how to run it from Java with ScriptEngine. The linked Java magazine also doesn't explain it. The article says the following:
To activate ES6 support, use
--language=es6
on the command line.
This does work with jjs
, but I can't find a way how to enable 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();
}
It fails with the following exception:
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've 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());
}
This outputs just the following:
-----------------------
ECMAScript
ECMA - 262 Edition 5.1
Does this mean I can't run ECMAScript 6 from Java and only using jjs
? Or is there something I have missed?
Thanks beforehand.