Search code examples
javabeanshell

BeanShell command line interpreter features


I'm trying to test BeanShell's command line interpreter in how it processes basic Java commands and syntax on my machine, and see if I can customise its behavior in any way. I've installed version 2.0b4 on my machine running OS X 10.10.1 (the JAR file is in /Library/Java/Extensions as per the instructions).

It's the closest thing to what I've been looking for, an interactive Java interpreter, but it doesn't have some standard features which a good interpreter should have.

  1. I'd like to be able to use the Up arrow key to reuse a previous command, but at the moment it doesn't recognise it, it just shows a control sequence. Is there a way to customise this for BeanShell?

  2. Is there a way to get BeanShell to print out the value of a variable if I've created it beforehand, just by naming it, like

    String s = new String( "Hello World!" ); s; Hello World!.

This is possible in Python for example.

  1. According to the documentation on importing Java classes which(<java class>); should return the classpath location of the specified Java class. But which( java.lang.String ); does not work for me, I get a NullPointerException:

    bsh % which(java.lang.String);
    Start ClassPath Mapping Mapping: Directory /Users/srm // Error: // Uncaught Exception: Method Invocation cp.getClassSource : at Line: 42 : in file: /bsh/commands/which.bsh : cp .getClassSource ( className )

    Called from method: which : at Line: 8 : in file: : which ( java .lang .String ) Target exception: java.lang.NullPointerException

    java.lang.NullPointerException

Any pointers or help would be appreciated.


Solution

    1. Run beanshell with jline.

    Download jline jar from http://jline.sourceforge.net/index.html and then you can do:

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

    Line editing capability will be provided by jline. I found this hint here.

    There are issues running with jline2. First, you'll get:

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

    Due to this issue which is fixed. But then, use the new class and you still get:

    $ 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
    

    due to this issue which is not fixed yet.

    1. Use show() command which will trigger showing of value.
    bsh % show();
    bsh % String s = new String("Hello World");
    bsh % s;
    <Hello World>
    bsh %
    

    It is mentioned in the Useful BeanShell Commands section of the documentation.

    1. Doesn't work for me either

    It doesn't fail in my case, but it didn't find it either.

    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 %