简介
安装JDK6以后,如果将JDK6的bin目录设置到Path变量中(windows下),那么打开命令行窗口,可以运行如下程序:
- jrunscript
jrunscript是什么呢,是java下的命令行脚本工具,command line script shell。
jrunscript是java6新增的工具,详细文档,参考Java6 Doc:/docs/technotes/tools/share/jrunscript.html
默认情况下,该工具解释执行javascript脚本。
执行的方式有两种,交互的方式和批处理方式。
该工具也可以解释执行其他脚本语言,如果配置了该语言的解释引擎,比如groovy,ruby,beanshell等。
简单使用
交互式使用
- D:\>jrunscript
- js> var time=new Date();
- js> println(time);
- Sun Nov 11 2007 16:43:37 GMT+0800 (CST)
- js>println(java.text.SimpleDateFormat('yyyy-MM-dd').format(time));
- 2007-11-11
- js>
为什么没有使用alert()?那是浏览器中才有的对象方法(window.alert()),这里只支持javascript的核心语法和功能,不过,可以通过这里的javascript直接访问java对象。
退出交互环境:
- exit();
批处理使用
创建js文件:
- var time=new Date();
- println(java.text.SimpleDateFormat('yyyy-MM-dd').format(time));
运行:
- E:\>jrunscript sample.js
- 2007-11-11