从java调用包含'const'的javascript?

问题描述 投票:0回答:3

我正在尝试使用一个用 js 开发并在 GitHub 上提供的转译工具。这个转译器能够将 javascript 代码转换为 java。

我想通过java代码调用该工具,以便我可以在java中读取js文件并使用JS开发的转译器对其进行处理。该工具使用“const”作为其代码的一部分。使用 ScriptEngine 时它给了我一个例外。请帮忙!!

示例代码:

public static void main(String[] args) {

    ScriptEngineManager manager = null;
    ScriptEngine engine = null;
    File folder = null;

    try {

        manager = new ScriptEngineManager();
        engine = manager.getEngineByName("nashorn");
        String testConst1 = (String) "const pi = 3.14;";
        String testPrint1 = (String) "function hello(name) {print ('Hello, ' + name +' = '+ pi);}";
        engine.eval(testConst1);
        engine.eval(testPrint1);

        Invocable inv = (Invocable) engine;
        inv.invokeFunction("hello", "pi");
        // System.out.println(); //This one works.

    } catch (Exception e) {
        e.printStackTrace();
    }
}

这段代码给了我下面的堆栈跟踪。

javax.script.ScriptException: <eval>:1:0 Expected an operand but found const
const pi = 3.14;
^ in <eval> at line number 1 at column number 0
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:522)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:509)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:152)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:0 Expected an operand but found const
const pi = 3.14;
^
at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:292)
at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:277)
at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3175)
at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3275)
at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1152)
at jdk.nashorn.internal.parser.Parser.statement(Parser.java:969)
at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:775)
at jdk.nashorn.internal.parser.Parser.program(Parser.java:711)
at jdk.nashorn.internal.parser.Parser.parse(Parser.java:284)
at jdk.nashorn.internal.parser.Parser.parse(Parser.java:250)
at jdk.nashorn.internal.runtime.Context.compile(Context.java:1207)
at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1179)
at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:594)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:520)
... 5 more
javascript java nashorn transpiler
3个回答
8
投票

使用 --language=es6 选项,nashorn 支持“const”关键字。

$ jjs --language=es6 
jjs> const x = 44

如果您以编程方式创建 Nashorn 脚本引擎,您仍然可以使用“nashorn.args”系统属性将选项传递给 Nashorn。

文件Main.java:

import javax.script.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine e = m.getEngineByName("nashorn");
        e.eval("const X = 44; print(X)");
    }
}


$ javac Main.java

$ java Main

Exception in thread "main" javax.script.ScriptException: <eval>:1:0 Expected an operand but found const
const X = 44; print(X)
^ 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:155)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at Main.main(Main.java:7)

$ java -Dnashorn.args=--language=es6 Main
44

参见此处 -> https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes


1
投票

您使用的脚本引擎是什么?

无论如何,这是因为某些 JavaScript 引擎无法识别

const
关键字。 (例如:Nashorn 不认识它)。

在这本书中它说, “如果脚本使用 const 关键字(Nashorn 无法识别该关键字),则此选项可用。”

它已经提交了一些现有的错误,


0
投票

使用此虚拟机选项

-Dnashorn.args=--语言=es6

© www.soinside.com 2019 - 2024. All rights reserved.