Modes of Operation

There are currently five basic modes of operation for running BeanShell: We'll outline these in this and the coming sections.

BeanShell is also integrated into a number of other tools and development environments including the Emacs JDE and the NetBeans/Forte for Java IDE. Please see the web site for articles and information about using BeanShell within these third party tools.


You can use BeanShell to run scripts from the command line or enter statements interactively by starting the bsh.Interpreter class. (See "Quickstart" for instructions on adding BeanShell to your classpath.)

 java bsh.Interpreter [ filename ] [ arg ] [ ... ]  // Run a script file

There are a few options which can be passed to the Interpreter using Java system properties:


The bsh.Remote launcher is the equivalent of bsh.Interpreter, but runs the specified file in a remote BeanShell engine. The remote engine may be a servlet mode BeanShell engine (BshServlet) or a native server mode remote BeanShell instance (embedded interpreter).

bsh.Remote accepts a URL and filename as arguments:

// servlet mode URL
java bsh.Remote http://localhost/bshservlet/eval test1.bsh

// remote server mode URL
java bsh.Remote bsh://localhost:1234/ test1.bsh

An HTTP URL may be specified that points to an instance of BshServlet (See "Servlet Mode" for details). Or a native "bsh:" URL may be specified, pointing to an instance of the BeanShell interpreter running in remote server mode. At the time of this writing bsh: style URLs for accessing native remote server mode instances are not implemented.

In either case, bsh.Remote sends the script to the remote engine for evaluation. If Remote can parse the retun value of the script as an integer it will return the value as the exit status to the command line.

Interactive Use

One of the most popular uses for BeanShell is, of course, as a "shell" for interactive experimentation and debugging. BeanShell can be run in a GUI desktop mode that offers a number conveniences like command line history, cut & paste, and tools for interactive use such as a simple classbrowser. We'll talk about the GUI in "The BeanShell Desktop" later.

The BeanShell GUI is comprised mostly of a set of BeanShell scripts supplied in the JAR file and launched by the BeanShell desktop() command.

However BeanShell can also be run interactively in plain text on the command line.

 java bsh.Interpreter   // Run interactively on the command line

This is useful for quick "one liners"; however it does not offer creature comforts such as command line history and editing. We should note that some shells, such as the Windows environment, do command line history and editing automatically - providing these features for BeanShell.

You can exit from an interactive shell by typing Control-D.

The return statement is ignored in interactive mode (it does not exit the shell).

The .bshrc Init File

When run interactively, BeanShell looks for a startup file called ".bshrc" in the user's home directory. If the file is found it is sourced into the interactive shell. You can use this to perform setup for your interactive use. For example, to add additional default imports or to toggle on the show() command if you prefer that.

The location of the .bshrc file is determined by the Java system property "user.home", which has different locations under different operating systems. The following table lists common locations:

.bshrc File Location:

Win95/98 single user C:\Windows\.bshrc
Win98 Multiuser C:\Windows\Profiles\<username>\.bshrc
NT/2K C:\Winnt\Profiles\<username>\.bshrc