"Expected an operand but found const" when trying to run a Javascript script using Scriptengine

Asked

Viewed 260 times

0

I’m trying to execute a method javascript from a project java. The logic I used I picked up on the internet and the same is so:

final String javascript = "/home/douglas/workspace/node/nintendo-switch-eshop/index.js";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        engine.eval(Files.newBufferedReader(Paths.get(javascript), StandardCharsets.UTF_8));
        Invocable inv = (Invocable) engine;
        System.out.println(inv.invokeFunction("getShopsAsia"));

However it gives error in the 4th line when it will read the js. The error is as follows:

Exception in thread "main" javax.script.ScriptException: <eval>:1:0 Expected an operand but found const
const request = require("request");
^ in <eval> at line number 1 at column number 0
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:537)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
    at App.main(App.java:17)
Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:0 Expected an operand but found const
const request = require("request");
^
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:294)
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:279)
    at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3182)
    at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
    at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1150)
    at jdk.nashorn.internal.parser.Parser.statement(Parser.java:967)
    at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773)
    at jdk.nashorn.internal.parser.Parser.program(Parser.java:709)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:283)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249)
    at jdk.nashorn.internal.runtime.Context.compile(Context.java:1284)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1251)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:627)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:535)
    ... 5 more

Does anyone know how I can solve ?

  • 1

    Just a guess: the problem isn’t const? Since it’s a fairly new feature, maybe Scriptengine doesn’t support it. Have you tried running a script that uses, say, var?

  • @Pabloalmeida thanks for the feedback. I’m very new to the javascript subject. But I’ll try to do this with a simple Hello World.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.