Exception while using ES6 features (template strings) with Nashorn on JDK 9
Jim Laskey (Oracle)
james.laskey at oracle.com
Wed Apr 26 14:00:07 UTC 2017
You need to specify --language=es6 on the command line
> On Apr 26, 2017, at 10:25 AM, Mohammed Sanaulla <sanaulla123 at gmail.com> wrote:
>
> Hello All,
>
> I am using build b166 of JDK 9.
>
> And I tried to run the following code:
>
> ScriptEngineManager engineManager = new ScriptEngineManager();
> ScriptEngine engine = engineManager.getEngineByName("nashorn");
> engine.eval("function sum(a, b) { return a + b; }");
> System.out.println(engine.eval("sum(1, 2);"));
> engine.eval("var name = 'Sanaulla'");
> System.out.println(engine.eval("print(`Hello Mr. ${name}`)"));
>
> The sum(1,2) works fine by printing 3. But the other part i.e using
> template strings throws the following exception:
>
> Exception in thread "main" javax.script.ScriptException: <eval>:1:6
> Expected an operand but found error
> print(`Hello Mr. ${name}`)
> ^ in <eval> at line number 1 at column number 6
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:469)
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:536)
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:523)
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:154)
> at
> java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
> at
> nashorn.demo/com.packt.JavascriptCodeFromJavaDemo.main(JavascriptCodeFromJavaDemo.java:13)
> Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:6
> Expected an operand but found error
> print(`Hello Mr. ${name}`)
> ^
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:297)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:282)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:4443)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4601)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.conditionalExpression(Parser.java:4753)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.assignmentExpression(Parser.java:4692)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.argumentList(Parser.java:3706)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:3389)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:4421)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4601)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.conditionalExpression(Parser.java:4753)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.assignmentExpression(Parser.java:4692)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4570)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expression(Parser.java:4566)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1847)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.statement(Parser.java:1155)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:909)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.program(Parser.java:844)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:325)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.parser.Parser.parse(Parser.java:285)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compile(Context.java:1500)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1467)
> at
> jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Context.compileScript(Context.java:750)
> at
> jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:534)
> ... 5 more
>
> I even tried running template strings from jjs console, but I am facing
> same issue.
>
> Can someone please guide me in using the ES6 features in Nashorn on JDK9?
>
> Regards,
> Sanaulla
More information about the nashorn-dev
mailing list