gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:58:45 25 4
gpt4 key购买 nike

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

我想通过java代码调用该工具,这样我就可以在java中读取js文件并使用JS开发的transpiler对其进行处理。该工具使用“const”作为其代码的一部分。在使用 ScriptEngine 时,它​​给了我一个异常(exception)。请帮忙!!

示例代码:

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

最佳答案

使用 --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

关于javascript - 从java调用包含 'const'的javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com