From Bauman National Library
This page was last modified on 8 June 2016, at 21:51.
Paradigm Object-oriented, scripting
Designed by JCP
Typing discipline Dynamic, strong
Implementation language Java
Platform JVM
License Apache v2.0,SPL / LGPL (Versions 2.0b4 and earlier)[1][2]

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

You can use BeanShell interactively for Java experimentation and debugging as well as to extend your applications in new ways. Scripting Java lends itself to a wide variety of applications including rapid prototyping, user scripting extension, rules engines, configuration, testing, dynamic deployment, embedded systems, and even Java education.

BeanShell is small and embeddable, so you can call BeanShell from your Java applications to execute Java code dynamically at run-time or to provide extensibility in your applications. Alternatively, you can use standalone BeanShell scripts to manipulate Java applications; working with Java objects and APIs dynamically. Since BeanShell is written in Java and runs in the same VM as your application, you can freely pass references to "live" objects into scripts and return them as results.

In short, BeanShell is dynamically interpreted Java, plus a scripting language and flexible environment all rolled into one clean package.

Language capabilities

  1. allows you to define functions that can be called from the script
  2. allows you to execute code written for the Java compiler, and BeanShell code can be compiled using the Java compiler.
  3. thanks to previous opportunities, can and is used for writing tests and debugging
  4. BeanShell provides a simple API for integration
  5. can be used in command line mode, without graphical environment

The history

The first versions of BeanShell (0.96, 1.0) were released by Patrick Niemeyer in 1999, followed by a series of versions. BeanShell 1.3.0 was released in August 2003. Version 2.0b1 was released in September 2003, culminating with version 2.0b4 in May 2005, which as of January 2015 is the newest release posted on the official webpage.

BeanShell has been included in the Linux distribution Debian since 1999.

BeanShell was undergoing standardization through the Java Community Process (JCP) under JSR 274.

Following the JCP approval of the BeanShell JSR Review Ballot in June 2005, no visible activity was taking place around BeanShell. The JSR 274 status is "Dormant".

Java REPL (JShell) is currently targeted for Java 9.

A fork of BeanShell, BeanShell2, was created in May 2007 on the Google Code website. The beanshell2 project has made a number of fixes and enhancements to BeanShell and multiple releases. As of January 2015, the latest version of beanshell2 is v2.1.8 released February 2014.

In December 2012, following a proposal to accept BeanShell as an Apache incubator project, BeanShell was licensed to the Apache Software Foundation and migrated to the Apache Extras, changing the license to Apache License 2.0. The project was accepted, and projected to become part of the Apache Commons.

Due to changes in the developers' personal circumstances, the BeanShell community did however not complete the move to Apache, but remained at Apache Extras. The project has since released BeanShell 2.0b5, which is used by Apache OpenOffice and Apache Taverna.

A Windows automated installer, BeanShell Double-Click, was created in 2013. It includes desktop integration features.

The BeanShell language syntax

The BeanShell language includes most of the constructs of the Java language. Standard Java constructs supported by the BeanShell include variable assignments, method calls, math expressions, for-loops and etc.

The first program

This programm print string on the terminal:

helloWorld() { 
  print("Hello World!");

Import classes

By default, the BeanShell imports the Java core classes at startup. You can import additional classes, using standard Java import syntax, for example,

    import mypackage.*;


    import mypackage.MyClass;

Declaring variables

The BeanShell allows you to create a special type of variable named a script variable. Unlike a standard Java variable, which can reference objects only of a specified type, a script variable can be defined to reference any type of object, including primitive types, such as int and boolean. You create a script variable by declaring it with or without a type specifier. If you include a type specifier, the variable can reference only values of the specified type. If you do not specify a type, the variable can reference values of any type. For example, the following statement

    foo = new Button("Another Button");

creates an untyped script variable named foo and assigns it a Button object. You are free to subsequently assign foo to any other type of object. This language, unlike java is not type safe and allows for dynamic typing, therefore it can be as the following variable declaration:

  Hashtable hashtable = new Hashtable();
  Date date = new Date();
  int value = 4;

and this:

  hashtable = new Hashtable(); 
  date = new Date();
  value = 4;

Undefined variables

You can test to see if a variable is "undefined" with the value void, e.g.:

    if ( foobar == void )
        // undefined

You can return a defined variable to the undefined state using the unset() command:

    a == void;  // true
    a = 5;
    unset("a"); // note the quotes
    a == void;  // true

Script methods

BeanShell lets you define and use a special type of method called a script method. Script methods differ from standard Java methods in the following ways:

  • Script methods are methods of an implicit, typeless object
  • Script methods can be defined to accept and return values of any type
  • Script methods can define other script methods

You use standard Java syntax to declare a script method that accepts and returns specific types. For example, the following code

    int addTwoNumbers( int a, int b ) {
        return a + b;

defines a BeanShell method called addTwoNumbers that accepts and returns values of type int. The next example

    int a = addTwoNumbers( 5, 7 );

uses the newly defined method to add two values of type int. You define an untyped script method by omitting type specifications. For example, the following statement

    add( a, b ) {
        return a + b;

defines a script method that accepts arguments of any type. When you invoke an untyped script method, BeanShell interprets the method based on the types of the arguments that you pass to the method. Consider, for example, the following invocations of the untyped add method defined in the preceding example:

    foo = add(1, 2);
    print( foo ); // 3

    foo = add("Oh", " baby");
    print( foo ); // Oh baby

The first invocation returns the result of adding, the second, of concatenating the arguments.

Methods with unspecified return types may return any type of object or no object. A return statement is optional. If omitted, the method returns the value of the last statement or expression in the method body. == Scripting objects == In the language BeanShell, you cannot create your own objects, such as understood in object-oriented languages, BeanShell each function or variable object. Often lack standard facilities of the Java language, and if necessary, you can write new ones using Java and use it in BeanShell context.

Implicit objects

The methods and variables defined by a script method are considered to be methods and fields of an implicit object. The reserved identifiers, this, super, and global, refer, respectively, to the current object, the calling object, and the global object. A method can access any variable or method in these scopes by qualifying the variable's name with the name of the appropriate implicit object.

    a = 42;
    foo() {
        a = 97;
        print( a );
        print( this.a );
        print( super.a );

    foo();  // prints 97, 97, 42

A script method can return its implicit object, thereby allowing the invoking script to access variables and methods defined by the method, using standard Java "." notation. For example,

    foo() {
        int a = 42;
        bar() {
            print("The bar is open!");
        return this;
    obj = foo();     // prints "the bar is open!"
    print ( obj.a )  // 42;       // prints "the bar is open!"

This reference

This reference plays a huge role in scripting languages. When you define variables within a block(function), these variables are associated with the unit and can be polcini through a reference to the object that owns:

 foo() {
     int bar = 42;
     return this;
 fooObject = foo();
 print( ); // prints 42!

In addition to this, objects can belong to the following scopes: 'this', 'super' and 'global'.


To synchronize the operations in the block or method is a keyword: synchronized. Examples:

 synchronized ( this ) { }   
 synchronized int foo () { }   
 synchronized int bar () { }   
 int gee() {
       synchronized( super ) { } 


For to take advantage of the multithreaded execution of our scripts, we need to implement the run() method and pass a reference to this object in constructor of Thread class:

 foo() {
    run() {
       // do work...
    return this;
 foo = foo();
 // Запуск двух потоков
 new Thread( foo ).start();
 new Thread( foo ).start();


  1. Programming language BeanShell
  2. BeanShell. Simple Java Scripting
    1. Niemeyer, Pat. "BeanShell [[Software]] License". Retrieved January 13, 2016.  URL–wikilink conflict (help)
    2. "BeanShell". Retrieved January 13, 2016.