After posting my other note, it occurred to me that you're probably trying to use ES6 on a system where a user can add plugins and click a button to restart, without access to the command-line. Admin UI's like that generally include a textbox for command-line switches. But OK, you asked for something beyond that, so....
There might be a couple ways to do this.
First, define arguments, and then just start the engine as normal:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
...
System.setProperty("nashorn.args", "--language=es6");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
That will append options at the end of the command-line that starts the engine. I've seen that syntax in forums.
The args may also be set like this (not sure):
Properties properties = System.getProperties();
properties.setProperty("nashorn.args", "--language=es6");
That may not work if permissions are restricting our ability to set properties on System. So...
A second approach is to pass the arguments into a factory method that returns an engine:
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
...
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new String[]{"-strict","--language=es6"});
For some docs on those switches:
(
http://hg.openjdk.java.net/jdk9/jdk9/nashorn/file/17cc754c8936/docs/DEVELOPER_README)
Note that is v9.
The thing is, I don't see the --language option mentioned explicitly in the above reference. And I don't see any references to ES6. Your challenge is to see if this works, and now with the above examples, to see if you can find any doc on it. XD
Here's a source of info that shows the factory approach.
(
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes)
And here is another source that shows both the Properties approach and the factory being used together.
(
http://hg.openjdk.java.net/jdk9/dev/nashorn/rev/d6b5c2c6c1d0#l5.113)
For anyone who wants to use ScriptCraftJS, the information in the OpenJDK wiki is like an Enchanted Diamond Axe if you want to conquer Nashorn. Start here:
(
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+Documentation)
And if you want to see great JavaScript with Java examples, here are samples right from the JDK10 source!
(
http://hg.openjdk.java.net/jdk10/jdk10/nashorn/file/3397ed166912/samples)
This might be another way to get an engine:
for (ScriptEngineFactory afactory : new ScriptEngineManager().getEngineFactories()) {
System.out.println(afactory.getLanguageVersion());
}
I'm guessing you'll get two in v9. Use the info there to save the ES6 instance if you find it.
I mention this because I can't find a single reference at OpenJDK or Oracle for "ES6", not even in JDK10 info:
(
https://docs.oracle.com/javase/10/scripting/toc.htm)
It's possible that we automatically get whatever ES6 abilities they have built-in over ES5. It might all be "nashorn", there might not be a type "es6".
With the above code, you can iterate over whatever is available in the factory to get the best engine. That means, after all of the above about setting properties, that we may not need to specify the 'language' explicitly at all.
I'm eager to know if any of this is helpful.
Regards.