HelloWorld 善战者,求之于势,不责于人;故能择人而任势。

知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得。物有本末,事有终始。知所先后,则近道矣。

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 40 评论 :: 0 Trackbacks

import java.io.File;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptTest {
 public static void main(String[] args) throws Exception {
  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine engine = manager.getEngineByName("JavaScript");
  testScriptVariables(engine);// 演示如何暴露Java对象为脚本语言的全局变量
  testInvokeScriptMethod(engine);// 演示如何在Java中调用脚本语言的方法
  testScriptInterface(engine);// 演示脚本语言如何实现Java的接口
  testUsingJDKClasses(engine);// 演示脚本语言如何使用JDK平台下的类
 }

 public static void testScriptVariables(ScriptEngine engine)
   throws ScriptException {
  File file = new File("e:/test/aaa.txt");
  engine.put("f", file);
  engine.eval("println('Total Space:'+f.getTotalSpace())");
 }

 public static void testInvokeScriptMethod(ScriptEngine engine)
   throws Exception {
  String script = "function hello(name) { return 'Hello,' + name;}";
  engine.eval(script);
  Invocable inv = (Invocable) engine;
  String res = (String) inv.invokeFunction("hello", "Scripting");
  System.out.println("res:" + res);
 }

 public static void testScriptInterface(ScriptEngine engine)
   throws ScriptException {
  String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
  engine.eval(script);
  Object obj = engine.get("obj");
  Invocable inv = (Invocable) engine;
  Runnable r = inv.getInterface(obj, Runnable.class);
  Thread th = new Thread(r);
  th.start();
 }

 public 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(js);
  Invocable inv = (Invocable) engine;
  inv.invokeFunction("doSwing", "Scripting Swing");
 }
}



</script>

posted on 2007-11-16 09:32 helloworld2008 阅读(534) 评论(0)  编辑  收藏 所属分类: java - 脚本

只有注册用户登录后才能发表评论。


网站导航: