通过使用 Java Scripting API,我能够在 Java 中执行 JavaScript。但是,有人可以解释一下我需要添加到此代码中的内容,以便能够调用 C:/Scripts/Jsfunctions.js 中的函数
import javax.script.*;
public class InvokeScriptFunction {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// JavaScript code in a String
String script1 = "function hello(name) {print ('Hello, ' + name);}";
String script2 = "function getValue(a,b) { if (a===\"Number\") return 1;
else return b;}";
// evaluate script
engine.eval(script1);
engine.eval(script2);
Invocable inv = (Invocable) engine;
inv.invokeFunction("hello", "Scripting!!"); //This one works.
}
}