Quick Guide to Using Jython 2.0 with IBM's Bean Scripting Framework
by Ross Lonstein
My recent work is primarily in Java but the language doesn't immediately lend itself
to the sort of fast (and loose) prototyping that loosely typed "glue" languages
like Perl and Python allow.
The solution is to embed another language which has the desired strengths into the
Java VM. That challenge has already been undertaken by many projects and a list
can be found at Robert Tolksdorf's site.
Making use of these languages is aided by an IBM AlphaWorks project known as the
Bean Scripting Framework (BSF).
From IBM's site, The Bean Scripting Framework (BSF) is an architecture for
incorporating scripting into Java applications and applets. Scripting languages
Rexx are commonly used to augment an application's function or to script together
a set of application components to form an application. There is great flexibility
in the BSF design and, aside, I hope to experiment more with it.
The language I chose for my tasks is Python.
I made this decision after surveying several of the
languages for the JVM and casually talking with my teammates about what they
wanted in a programming language. We agreed that power, clarity, simplicity and availability
of modules were the most important factors and JPython,
a 100% pure Java implementation of the Python 1.5 language, fit the bill.
Jython, however, is the successor to JPython, and is based upon Python 2.0 and supports most of the standard library.
Aside from the obvious benefits of being more closely in step with current books and documentation
and handling many existing third-party modules without changes, Jython is under active development to
incorporate the features of Python 2.1. The only downside was that the BSF defaults to support
the earlier JPython but not the current Jython. The below steps will remedy that.
In addition to a working JDK, you will need the following:
It is not necessary to build Ant or Jython from source. You will want to build or include Ant's optional.jar
components since they include the interface to the BSF. Only the jars needs to be in the CLASSPATH but you
will need both a working Ant and Jython environment, so follow the instructions on the respective websites.
Note that if you are also using other scripting languages such as IBM's own NetREXX or Netscape's Rhino, those
libraries need to be in the CLASSPATH during the build or support for them is left out of the finished
In the build.xml file for BSF add the lines:
<available property="jython.present" classname="org.python.util.PythonInterpreter"/>
<target name="checkJython" unless="jython.present">
<echo message="Warning: Jython dependencies were not resolved."/>
Remove the lines with references to "JPython" since they will conflict with the "Jython" components.
Edit the Languages.properties file in the subdirectory /build/src/com/ibm/bsf
of your BSF source directory adding:
jython = com.ibm.bsf.engines.jython.JythonEngine, py
Again, remove the lines referring to "JPython".
Rename thejpython directory in /src/com/ibm/bsf/engines/ to jython.
Rename the Jpython.Engine file to Jython.Engine.
Edit the newly renamed file and globally replace 'Jpython' with 'Jython', making sure
that the class name matches the filename. Save the file.
From a command prompt, go to the directory containing your BSF sources
and execute the command 'ant'.
This will build the BSF classes and assemble them into a .jar file.
Since we have Ant already, testing is a simple affair:
- In a temporary directory, create a new build.xml file.
- Add the following to the file:
<project name="testcase" default="jython-test">
<script language="jython"> <![CDATA[
print 'Hello, world!'
a,b = 0,1
result = 
while b < n:
a,b = b,a+b
print 'Now a quick Fibonacci sequence! ',
print 'And something about ourselves:'
print 'This Python is',sys.copyright,'Version', sys.version,
print 'for ',sys.platform,' with the following built in', sys.builtin_module_names
- Save it and execute ant in the current directory. You should see something similar to:
Searching for build.xml ...
*sys-package-mgr*: processing new jar, 'C:\java\jython-2.0\jython.jar'
*sys-package-mgr*: processing new jar, 'C:\java\libs\jakarta-ant\lib\ant.jar'
*sys-package-mgr*: processing new jar, 'C:\java\libs\jakarta-ant\lib\bsf.jar'
*sys-package-mgr*: processing new jar, 'C:\java\libs\jakarta-ant\lib\jaxp.jar'
*sys-package-mgr*: processing new jar, 'C:\java\libs\jakarta-ant\lib\optional.jar'
*sys-package-mgr*: processing new jar, 'C:\java\libs\jakarta-ant\lib\parser.jar'
Now a quick Fibonacci sequence! [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
And something about ourselves:
This Python is Copyright (c) 2000, Jython Developers
All rights reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 2000 The Apache Software Foundation. All rights
Copyright (c) 1995-2000 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
Languages for the Java VM
Python Language Website
Apache Jakarta Ant tool
JavaWorld - Script JavaBeans with BSF