Java Programming


I ran across BeanShell when I was looking for a scripting language that can interface with my Java code. My primary motivation for seeking a scripting language is that I wanted to be able to test the individual components of my application without have to launch the entire app-server. BeanShell allows me to quickly write a script that can load my java classes and perform my test and since it is interpretive there is no need to constantly recompile the script (a minor step, yes, but still a time saver).

BeanShell is probably not as well known as Jython, the Java implementation of the language Python, but one advantage it has over Jython is that you don't need to learn another language in order to use it. BeanShell uses Java as its language, so if you know Java, you know how to program with BeanShell.

The one weakness in BeanShell is the lack of documentation, which is the main reason for this entry. I'll stick notes here on my experiences with BeanShell.

Originally, I was hoping to find something like Jython except using Perl as the language, but besides some early stage work which seems to have been abandoned, I couldn't find anything.

+ Arguments passed to a BeanShell script is stored in the array, bsh.args. You can access the arguments this way:

    bsh.args[0], bsh.args[1]..

+ Additional commands in BeanShell that's not as well documented (from the BeanShell mailing list):

    which(class)
      Maps the classpath and reports the (first) location of the directory, zip or Jar file in which the class is found.

      e.g. which( java.lang.String );
      e.g. which( "java.lang.String" );
      e.g. which( java.lang.String.class );

    getSourceFileInfo()
      returns the name of the file (or other source) from which the interpreter is currently reading.

    source( URL )
      e.g. source( getResource("/lib/foo.properties") ); an addition form of source() command which takes a URL as an argument.


| back to programming |

©2002 hsin tsao