日期:2014-05-16 浏览次数:20413 次
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestJs {
?public static String test() throws Exception {
??// 读取JS文件
??BufferedReader buf = new BufferedReader(new InputStreamReader(
????new FileInputStream(new File("js/test.js"))));
??// 调用js。。这里是关键 啊
??ScriptEngineManager scriptManager = new ScriptEngineManager();
??ScriptEngine js = scriptManager.getEngineByExtension("js");
??// 执行JS
??js.eval(buf);
??Invocable inv = (Invocable) js;
??// p1的获取 执行js中的方法
??String p1 = (String) inv.invokeFunction("test");
??return p1;
?}
?public static void main(String[] args) throws Exception {
??System.out.println(test());
?}
}
?