在jdk1.6之後版本中新增了ScriptEngine類,允許Java程序直接調用javascript代碼
Java代碼
package org.mystyle.js;
import java.io.File;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class InvokingJavaScript {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String fileName = "src/demo.js";
File file = new File(fileName);
FileReader reader = new FileReader(file.getAbsolutePath().replaceAll("\\\\", "/"));
engine.eval(reader);
if(engine instanceof Invocable){
Invocable invoke = (Invocable) engine;
Double sum = (Double) invoke.invokeFunction("addFun", 2, 3);
System.out.println(sum);
}
reader.close();
}
}
javascript文件demo.js
function addFun(a, b){
return a+b;
}
輸出:
5.0