07 February 2008

BeanShell 01

I first ran across BeanShell (bsh) in early 2006 while looking for a way to better automate an Ant build. For the first few months, it was pure love. I couldn't get enough and I drove everyone at work nuts with my constant white board examples and endless emails. JSR 274 "The BeanShell Scripting Language" had recently been passed and I was sure that everyone would soon be loving Beany. Wikipedia says "BeanShell is an open source project and has been incorporated into many applications, such as OpenOffice.org, Apache Ant, BEA WebLogic Application Server, jEdit, and many others". Unfortunately, I haven't seen anything new written about bsh. The developer of BeanShell, Pat Niemeyer, hasn't updated the website not made a new release forever. I've sent Pat a few emails to ask about status but haven't received a reply. I guess the project is dead which is too bad because they're some really cool things that you can do with bsh.

So... what cool things can be done you ask? Patience grasshopper. Let's first get it installed. It'd be great if you'd review the "Getting Started" section of the bsh manual, but in a nutshell:

Grab the latest bsh jar which is currently 2.0b4. Either drop the BeanShell JAR file into your Java extensions folder or add it to your classpath:

Install as an extension. Place the bsh.jar file in your
    $JAVA_HOME/jre/lib/ext folder
Or add BeanShell to your classpath like this:
   nix: export CLASSPATH=$CLASSPATH:bsh-xx.jar
   windows: set classpath %classpath%;bsh-xx.jar

You can then run BeanShell in either a GUI or command line mode:
    java bsh.Console // run the GUI
    java bsh.Interpreter // run as text-only on the command line
    java bsh.Interpreter filename [ args ] // run a script file

It's also possible to call bsh from within native Java applications, to reach it in a remote server mode for debugging (very cool), to use it as a servlet, or even in an applet.

Let's check that it's installed and do some basic stuff. From a cmd line, type java bsh.Console and you should see the GUI start up and then have a workspace like the picture to the right. If not, check your classpath and try again.

I'm a hacker at heart so one of the first things that I did was to fire up the class browser (in the workshop, type classBrowser(); ) and started explorer classes in my classpath. This simple tool has helped me understand APIs and undocumented methods for many classes. Just select a package and then class and the browser will reveal the constructor(s), method(s), and field(s). Plus you can see the file system path of the class which has helped me several times when I had classes loaded multiple times.

There's much more to talk about but this entry is getting long. Next time, I'll discuss more Console commands and some bsh examples. In the mean time, download bsh and read thru the manual. I think you'll agree - BeanShell has so much potential.

No comments: