java有一个脚本管理器,允许java调用javascript,如下所示:
import javax.script.*;
public class ExecuteScript {
public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
engine.eval("print('Welocme to java world')");
}
public static void sayHi(){
System.out.println("hihi");
}
}
我的问题是,如果我有一个sayHi()函数,我可以使用javascript,通过脚本引擎来调用Java函数吗?谢谢。
答案 0 :(得分:8)
以下代码段
package org.test.script;
import javax.script.*;
public class ExecuteScript {
public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
engine.eval("" +
"importPackage(org.test.script);\n" +
"print('Welocme to java world\\n');\n" +
"ExecuteScript.sayHi();");
}
public static void sayHi() {
System.out.println("hihi");
}
}
输出
Welocme to java world
hihi
答案 1 :(得分:5)
快速从JavaDocs中攻击。
import javax.script.*;
public class ExecuteScript {
public static void main(String[] args) throws Exception {
// create a Java object
ExecuteScript es = new ExecuteScript();
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
engine.eval("println('Welcome to Java world')");
// add the Java object into the engine.
engine.put("es",es);
ScriptEngineFactory sef = engine.getFactory();
String s = sef.getMethodCallSyntax("es", "sayHi", new String[0]);
// show the correct way to call the Java method
System.out.println(s);
engine.eval(s);
}
public static void sayHi(){
System.out.println("hihi");
}
}
Welcome to Java world
es.sayHi()
hihi
Press any key to continue . . .
答案 2 :(得分:1)
我不确定您使用的是哪个脚本管理器,但使用Rhino可以执行以下操作
var date = new java.util.Date();
print(date);
因此,使用您的示例,您应该能够像静态方法一样调用它:
ExecuteScript.sayHi();
答案 3 :(得分:1)
这是Java 8 Nashorn的一个小例子,它可以作为没有任何软件包的独立脚本工作:
import javax.script.*;
import java.util.LinkedList;
public class RunJavaFromJs {
public static void main(String[] args) throws Exception {
LinkedList<Integer> jList = new LinkedList<>();
jList.add((Integer)42);
ScriptEngine engine =
(new ScriptEngineManager())
.getEngineByName("nashorn");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("jList", jList);
engine.eval("" +
"load('nashorn:mozilla_compat.js');\n" +
"// call static method\n" +
"var i = Packages.RunJavaFromJs.helloWorld(42);\n" +
"// perform side effect\n" +
"print('Result was: ' + i + '(printed from JS)');\n" +
"// call method on `jList` instance passed in bindings \n" +
"jList.add(12345);\n" +
"print(jList.toString());"
);
}
public static int helloWorld(int x) {
System.out.println("Hello, world! (printed from Java in static method)");
return x * x;
}
}
如果仅将其保存在RunJavaFromJs.java
中,则使用编译并运行
javac RunJavaFromJs.java && java RunJavaFromJs
然后您获得:
Hello, world! (printed from Java in static method)
Result was: 1764(printed from JS)
[42, 12345]