Shell_BeanShell快速入门---Java应用程序脚本引擎

下载和运行BeanShell

请到http://www.beanshell.org下载最新的JAR文件。你可以用图形桌面模式和命令行模式起动BeanShell。
如果你只是要玩一玩BeanShell,你可以在BeanShell的jar文件上双击来起动BeanShell的桌面。但不管怎样,如果你要让BeanShell与你的类与应用程序一起工作就必须将BeanShell的jar文件加到classpath中。
你可以将BeanShell的jar文件拖到JAVA_HOME的ext目录也可以直接加到classpath中。

·        windows用户请将bsh.jar放在JAVA_HOME/jre/lib/ext文件夹,OSX用户可以放在/Library/Java/Extensions.
或者增加BeanShell到你的classpath目录,如:
unix: export CLASSPATH=$CLASSPATH:bsh-xx.jar
windows:set classpath %classpath%;bsh-xx.jar

然后你就可以运行BeanShell在GUI或命令行模式:

·         javabsh.Console       // run the graphical desktop
or
     java bsh.Interpreter   // run as text-onlyon the command line
or
     java bsh.Interpreter filename [ args ] // run scriptfile

可以在你的应用程序内部来运行,也可以作为debug及servlet的远程服务器模式,或一个Applet内部来运行BeanShell。请参考"BeanShellModes of Operation"获得更多详情。


完整代码:
package cn.com.sparknet.util;

import bsh.*;
import java.util.*;

public class BeanShell {
public static void main(String[] args) {
try {
Interpreter interpreter = new Interpreter(); // 构造一个解释器
interpreter.set("foo", 5); // 设置变量
interpreter.set("date", new Date()); //设置一个时间对象
Date date = (Date) interpreter.get("date"); // 重新得到时间变量
interpreter.println(date.toString()); //打印时间变量
interpreter.eval("bar = foo*10"); // 对一段脚本求值,并得到结果
System.out.println(interpreter.get("bar")); //打印变量
interpreter.source("d:\\helloWorld.bsh"); // 导入并执行一个脚本文件
}
catch (Exception e) {
//如果发生异常,写入日志文件
Log.error(new BeanShell(), "main", FormatDate.getCurrDate(),e.getMessage());
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值