[ Prev ] [ Index ] [ Next ]

Bean Shell bsh

Created Friday 1/6/2007

Bean Shell (bsh) is a java interpretor that runs as a shell. The interpretor allows instances of classes that are defined in the bean shell jar (or classpath jar) to be created and executed. In this document, the beanshell prompt is bsh %

1. Starting the bsh

There are at least two ways to start the bean shell environment. If the bsh(1) script is installed, then this is the preferred option. If not then the java(1) jvm can be used as long as the classpath contains the beanshell jar. These two options are illustrated below:

//Start the beanshell from the terminal	
bash $ bsh
BeanShell 1.3.0 - by Pat Niemeyer (pat@pat.net)
bsh % System.exit(0); //// exit the beanshell or type ctrl+d//	
//Start the beanshell from the terminal using the jvm/
bash $ /usr/local/jdk/bin/java
//Launch the beanshell desktop environment	
bash $ /usr/local/jdk/bin/java -jar bsh.jar

2. Simple interaction

Whilst the bean shell environment is similar to the standard JDK, it is not identical. For example, variable declarations do not require the type (but are in fact typed). E.g., in java a string might be defined as String string = "foo";, whereas in bsh, it is simply string = "Foo";. The following examples illustrate some bsh commands and differences to the JDK.

bsh % foo = "Foo bar";    
bsh % four = (2 + 2)*2/2;
bsh % print( foo + " = " + four );  // print() is a BeanShell command	
Foo the bar = 4
bsh % System.err.println(""+foo);   // can also use System.err	
Foo bar
bsh % // currently the variable four is an int, however beanshell variables are untyped
bsh % four = "This is a string with " + four + " in it";
bsh % print(four);
This is a string with 4 in it
bsh % // Do a loop	
bsh % for (i=0; i<5; i++) {
bsh %     System.err.print(i + " "); // could also use print()	
bsh % }
0 1 2 3 4
bsh % System.exit(four);

3. Swing example

This example creates a simple frame with a JButton. The button has an ActionListener that prints the ActionEvent. The bsh commands for this option should be familiar and are pretty much 1:1 with writing a standard java class that opens a JFrame:

bsh % // Pop up a frame with a button in it
bsh % button = new JButton( "My Button" );
bsh % frame = new JFrame( "My Frame" );
bsh % frame.add( button, "Center" ); // jdk1.5: don't need to add to the content pane (frame "add" does this for us)
bsh % ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent ae) { System.err.println("COMMAND " + ae); } };
bsh % button.addActionListener(l);
bsh % frame.pack();
bsh % frame.setVisible(true);

4. Useful BeanShell Commands

There are several beanshell built-in commands, such as print(), already covered in bsh#2 above. The main differences between beanshell print() and the JDK System.out.print() is that print() output is more verbose (c.f., arrays) and is always directed to the terminal. There are many of these beanshell built-ins, some of which are listed below:

Stuart Moorfoot 1 June 2007 foo@bund.com.au


No backlinks to this page.