谁说JVM只能执行编译过后的class?(java语言)
JSH帮你轻松搞定shell脚本,这点现在已经成为了一个标准:JSR-274。能够应用于用动态规则改变业务流程的场景。
另:在这之前,还有Groovy(JSR- 241)也能在JVM中运行。
最新版本:2.0b4,官网:http://www.beanshell.org
例1:
public static void main(String[] args) {
Interpreter interpreter = new Interpreter();
String s = "2>3||3>4&&5<4";
try {
interpreter.set("flag", interpreter.eval("(" + s +")"));
System.out.println(interpreter.get("flag"));
} catch (EvalError e) {
e.printStackTrace();
}
}
例2:
- import bsh.Interpreter;
Interpreter i = new Interpreter(); // Construct an interpreter
// Source an external script file
Object obj = i.source("actionHandler.bsh");
| actionHandler.bsh - The scripted ActionListener |
import java.awt.event.ActionListener;
actionPerformed( e ) {
print("Button Pressed: " + e);
}
return (ActionListener)this; |
本文介绍如何使用JSH在JVM上执行Shell脚本,并提及了Groovy作为另一种能在JVM上运行的语言。通过示例展示了如何利用bsh.Interpreter进行表达式的解析与执行。
360

被折叠的 条评论
为什么被折叠?



