Mixing Java and Scripts with BSF

Problem

You want to interface Java components to an existing scripting language.

Solution

Use the Bean Scripting Framework (BSF).

Discussion

Many scripting languages are used in the computing field today: VB, Perl, Python, JavaScript, Tcl/TK, REXX, and others. A project that originated at IBM, the Bean Scripting Framework (BSF) aims to provide a way to allow all of them to interoperate with Java.

The BSF consists of a management API, an engine API for driving different scripting languages, and a series of plug-ins for different scripting languages. The management API lets you either evaluate an expression in the given scripting language, such as “2+2” (which is so simple as to be valid in most supported languages), or run a script stored in a script file. In this example I’ll use JPython, a pure-Java (certified) implementation of the scripting language Python (see http://www.python.org or the O’Reilly book Learning Python).

While it is convenient (and efficient) to run JPython in the same JVM as the calling program, this is not by any means a requirement; for example, it is possible to use BSF with scripting languages written in some native language. BSF and the scripting plug-in are responsible for dealing with whatever “plumbing” -- external connections or processes -- this requires. In fact, BSF currently supports the languages listed in Table 26-1.

Table 26-1. Languages supported by BSF

Language

Description

JPython

Java implementation of Python ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.