Skip to content
Stian Soiland-Reyes edited this page Feb 18, 2016 · 2 revisions

BeanShell commands appear to the user as pre-defined methods such as print(), load(), or save().

BeanShell Commands can be implemented as scripted methods or compiled Java classes which are dynamically loaded on demand from the classpath. We'll talk about adding your own commands in the next section "Adding BeanShell Commands".

Tip: You can easily override any BeanShell command simply by defining the method yourself in your script. For example:

print( arg ) {
  System.out.println( "You printed: " + arg );
}

If you define the method in the global scope it will apply everywhere. If you define it local to a scripted object it will only apply in that object context.

Commands Overview

This is a high level overview of the BeanShell command set. You can find full documentation for all BeanShell commands in the "BeanShell Commands Documentation" section of this manual. See also the "BshDoc" section which covers javadoc style documentation of BeanShell script files.

Interpreter Modes

The following commands affect general modes of operation of the interpreter.

exit() Exit the interpreter. (Also Control-D).
show() Turn on "show" mode which prints the result of every evaluation that is not of void type.
setAccessibility() Turn on access to private and protected members of Java classes.
server() Launch the remote access mode, allowing remote access to the interpreter from a web browser or telnet client.
debug() Turns on debug mode. Note: this is very verbose, unstructured output and is primarily of interest to developers.
setStrictJava() Turn on "strict Java" mode which enforces Java compatibility by dissallowing loose types and undeclared variables.

Output

The following commands are used for output:

print(), error() Print output to standard out or standard error. print() always goes to the console, whereas System.out may or may not be captured by a GUI console or servlet.
frame() Display the AWT or Swing component in a Frame

Source and Evaluation

The following commands are used for evaluation or to run external scripts or applications:

eval() Evaluate a string as if it were typed in the current scope.
source(), sourceRelative() Read an external script file into the interpreter and evaluate it in the current scope
run(), bg() Run an external file in a subordinate interpreter or in a background thread in a subordinate interpreter.
exec() Run a native executable in the host OS

Utilities

The following commands are useful utilities:

javap() Print the methods and fields of an object, similar to the output of javap
which() Like the Unix 'which' command for executables. Map the classpath and determine the location of the specified class.
load(), save() load a serializable object from a file or save one to a file. Special handling is provided for certain objects.
object() Create an "empty" object context to hold variables; analogous to a Map.

Variables and Scope

The following commands affect the current scope:

clear() Clear all variables, methods and imports from the current scope.
unset() Remove a variable from the current scope. (Return it to the "undefined" state).
setNameSpace() Set the current namespace to a specified scope. Effectively bind the current scope to a new parent scope.

Classpath

The following commands manipulate or access the classpath:

addClassPath(), setClassPath(), getClassPath() Modify the BeanShell classpath.
reloadClasses() Reload a class or group of classes.
getClass() Load a class explicitly taking into account the BeanShell classpath.
getResource() Get a resource from the classpath.

Files and Directories

The following commands work with files, directories, and the working directory:

cd(), pwd(), dir(), rm(), mv(), cat() Unix Style file commands.
pathToFile() Translate a relative path to an absolute path taking into account the BeanShell current working directory.

Desktop and Class Browser

The following commands work with GUI tools:

classBrowser(), browseClass() Open a class browser window or browse a specific class or object.
desktop() Launch the BeanShell GUI desktop.
setNameCompletion() Turn on or off name completion in the GUI console.

Note: The dir() command is written in Java; primarily as a demonstration of how to do this when desired.

/var/run/sh Using namespace kachu; int razbe[<missing9/2:3^]i9y-pi\ |`newl lne4:ash/COL?( i9 = struct )

 return()

20260104-7:48AM} Wylie:miLkGodX5.83_bi_41 = 1254| \condome

Clone this wiki locally