---
title: "jrunscript的使用"
author: "marshal"
date: "2007-11-11T16:57:13Z"
tags: []
slug: "jrunscript"
draft: false
lastmod: "2007-11-11T16:57:13Z"
id: 139766
url: "https://marshal.iteye.com/blog/139766"
---

## 简介

安装JDK6以后，如果将JDK6的bin目录设置到Path变量中（windows下），那么打开命令行窗口，可以运行如下程序：

1. jrunscript

jrunscript是什么呢，是java下的命令行脚本工具，command line script shell。

jrunscript是java6新增的工具，详细文档，参考Java6 Doc：/docs/technotes/tools/share/jrunscript.html

默认情况下，该工具解释执行javascript脚本。

执行的方式有两种，交互的方式和批处理方式。

该工具也可以解释执行其他脚本语言，如果配置了该语言的解释引擎，比如groovy，ruby，beanshell等。

## 简单使用

### 交互式使用

1. D:\\&gt;jrunscript
2. js&gt; var time=new Date();
3. js&gt; println(time);
4. Sun Nov 11 2007 16:43:37 GMT+0800 (CST)
5. js&gt;println(java.text.SimpleDateFormat('yyyy-MM-dd').format(time));
6. 2007-11-11
7. js&gt;

为什么没有使用alert()？那是浏览器中才有的对象方法（window.alert()），这里只支持javascript的核心语法和功能，不过，可以通过这里的javascript直接访问java对象。

退出交互环境：

1. exit();

### 批处理使用

创建js文件：

1. var time=new Date();
2. println(java.text.SimpleDateFormat('yyyy-MM-dd').format(time));

运行：

1. E:\\&gt;jrunscript sample.js
2. 2007-11-11