public static void ExcuteJs(String paras) throws ScriptException,
FileNotFoundException, NoSuchMethodException
{
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("JavaScript"); //得到脚本引擎
engine.eval("var a=1; var b=2;print (a+b);"); //执行运算
// engine.eval("alert(\"js alert\");"); // 不能调用浏览器中定义的js函数 // 错误,会抛出alert引用不存在的异常
engine.put("a", 4);
engine.put("b", 3);
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
try {
// 只能为Double,使用Float和Integer会抛出异常
Double result = (Double) engine.eval("a+b");
System.out.println("result = " + result);
engine.eval("c=a+b");
Double c = (Double)engine.get("c");
System.out.println("c = " + c);
engine.eval(new java.io.FileReader("d:/test.js"));
Invocable inv = (Invocable)engine;
Object a = inv.invokeFunction("test", paras ); //test.js中的test方法
CheckMethods.PrintDebugMessage(a.toString());
System.out.println(a.toString());
}