java中调用js脚本 学学还是有用的
(2011-09-18 10:08:52)关于ScriptEngine和ScriptEngineManager的使用:
mgr.getEngineByMimeType(String mimeType)
mgr.getEngineByName(String shortName)
getEngineByExtension可用参数:
js
eg:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByExtension("js");
application/javascript
application/ecmascript
text/javascript
text/ecmascript
eg:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByMimeType("application/javascript");
getEngineByName可用参数:
js
rhino
JavaScript
javascript
ECMAScript//js和javascript是它的扩展
eg:
ScriptEngine engine = mgr.getEngineByName("JavaScript");
具体解析js可以是加载js文件或者js 脚本字符串:
例如夹在js文件:
engine.eval_r(new FileReader(jsfile));
engine.eval_r(jsstring);
eg:
engine.eval_r("function test(){return 'this is test js in java'}");
Invocable inv = (Invocable) engine;
String value = String.valueOf(inv.invokeFunction("test"));
以下是测试:
-
- private
static void testUsingJDKClasses(ScriptEngine engine) throws Exception { -
// Packages是脚本语言里的一个全局变量,专用于访问JDK的package -
String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}"; -
engine.eval_r(js); -
// Invocable 接口: 允许java平台调用脚本程序中的函数或方法 -
Invocable inv = (Invocable) engine; -
// invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数; -
inv.invokeFunction("doSwing", "Scripting Swing"); - }
private static void testUsingJDKClasses(ScriptEngine engine) throws Exception {
// Packages是脚本语言里的一个全局变量,专用于访问JDK的package
String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
engine.eval_r(js);
// Invocable 接口: 允许java平台调用脚本程序中的函数或方法
Invocable inv = (Invocable) engine;
// invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;
inv.invokeFunction("doSwing", "Scripting Swing");
}
下面的代码段演示脚本语言如何实现Java的接口
-
- private
static void testScriptInterface(ScriptEngine engine) throws ScriptException { -
String script = "var obj = new Object(); obj.run = function() { println('run method called'); }"; -
engine.eval_r(script); -
Object obj = engine.get("obj"); -
Invocable inv = (Invocable) engine; -
-
Runnable r = inv.getInterface(obj, Runnable.class); -
Thread th = new Thread(r); -
th.start(); - }
private static void testScriptInterface(ScriptEngine engine) throws ScriptException {
String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
engine.eval_r(script);
Object obj = engine.get("obj");
Invocable inv = (Invocable) engine;
Runnable r = inv.getInterface(obj, Runnable.class);
Thread th = new Thread(r);
th.start();
}
演示如何在Java中调用脚本语言的方法
-
- private
static void testInvokeScriptMethod(ScriptEngine engine) throws Exception { -
String script = "function helloFunction(name) { return 'Hello everybody,' + name;}"; -
engine.eval_r(script); -
Invocable inv = (Invocable) engine; -
String res = (String) inv.invokeFunction("helloFunction", "Scripting"); -
System.out.println("res:" + res); - }
private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {
String script = "function helloFunction(name) { return 'Hello everybody,' + name;}";
engine.eval_r(script);
Invocable inv = (Invocable) engine;
String res = (String) inv.invokeFunction("helloFunction", "Scripting");
System.out.println("res:" + res);
}
演示如何暴露Java对象为脚本语言的全局变量
-
- private
static void testScriptVariables(ScriptEngine engine) throws ScriptException { -
File file = new File("test.txt"); -
engine.put("f", file); -
engine.eval_r("println('Total Space:'+f.getTotalSpace())"); -
engine.eval_r("println('Path:'+f.getPath())"); - }