【问题标题】:BeanShell command line interpreter featuresBeanShell 命令行解释器功能
【发布时间】:2015-01-24 12:46:08
【问题描述】:

我正在尝试测试 BeanShell 的命令行解释器如何在我的机器上处理基本的 Java 命令和语法,并看看我是否可以以任何方式自定义它的行为。我在运行 OS X 10.10.1 的机器上安装了 2.0b4 版本(按照说明,JAR 文件位于 /Library/Java/Extensions 中)。

它是我一直在寻找的最接近的东西,一个交互式 Java 解释器,但它没有一些好的解释器应该具备的标准功能。

  1. 我希望能够使用向上箭头键重用以前的命令,但目前它无法识别它,它只显示一个控制序列。有没有办法为 BeanShell 定制这个?

  2. 如果我事先创建了一个变量,有没有办法让 BeanShell 打印出它的值,只需命名它,比如

    String s = new String("Hello World!"); 小号; 世界你好!

这在 Python 中是可能的。

  1. 根据关于导入 Java 类的文档which(<java class>); 应该返回指定 Java 类的类路径位置。但是which( java.lang.String ); 对我不起作用,我得到一个NullPointerException

    bsh % which(java.lang.String);
    启动 ClassPath 映射 映射:目录/Users/srm // 错误:// 未捕获的异常:方法调用 cp.getClassSource:在第 42 行:在文件:/bsh/commands/which.bsh:cp .getClassSource (className)

    从方法调用:which : at Line: 8 : in file: : which ( java .lang .String ) 目标异常:java.lang.NullPointerException

    java.lang.NullPointerException

任何指针或帮助将不胜感激。

【问题讨论】:

标签: java beanshell


【解决方案1】:
  1. 使用 jline 运行 beanshell。

http://jline.sourceforge.net/index.html下载jline jar 然后就可以了:

java -cp jline-1.0.jar:bsh-2.0b4.jar jline.ConsoleRunner bsh.Interpreter

行编辑功能将由 jline 提供。我发现了这个提示here

jline2 存在问题。首先,你会得到:

 $ java -cp jline-2.12.jar:bsh-2.0b4.jar jline.ConsoleRunner bsh.Interpreter
 Exception in thread "main" java.lang.NoClassDefFoundError: jline/ConsoleRunner

由于this 问题已修复。但是,使用新的类,你仍然会得到:

$ java -cp jline-2.12.jar:bsh-2.0b4.jar jline.console.internal.ConsoleRunner bsh.Interpreter
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments

由于this 尚未修复的问题。

  1. 使用 show() 命令触发显示值。
bsh % show();
bsh % String s = new String("Hello World");
bsh % s;
<Hello World>
bsh %

文档的 Useful BeanShell Commands 部分中提到了它。

  1. 也不适合我

在我的情况下它并没有失败,但它也没有找到它。

bsh % which(java.lang.String);
Start ClassPath Mapping
Mapping: Archive: file:/Users/me/beanshell/jline-1.0.jar
Mapping: Archive: file:/Users/me/beanshell/bsh-2.0b4.jar
Mapping: Archive: file:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
End ClassPath Mapping
null
bsh %

【讨论】:

  • 谢谢,会检查一下。
  • 我尝试使用 jline2 并发现存在问题并且无法正常工作,所以现在最好坚持使用 jline1。我在答案中添加了详细信息。
  • 如果 jline1 稳定,那么我会继续这样做。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2013-12-10
  • 2011-04-08
相关资源
最近更新 更多