Quick Guide to Using Jython 2.0 with IBM's Bean Scripting Framework

by Ross Lonstein


Introduction

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 such as Netscape Rhino (Javascript), VBScript, Perl, Tcl, Python, NetRexx and 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.


Getting Started

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 library.


Steps

  1. 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."/>
         </target>
    Remove the lines with references to "JPython" since they will conflict with the "Jython" components.
  2. 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".
  3. Rename thejpython directory in /src/com/ibm/bsf/engines/ to jython.
  4. Rename the Jpython.Engine file to Jython.Engine.
  5. Edit the newly renamed file and globally replace 'Jpython' with 'Jython', making sure that the class name matches the filename. Save the file.
  6. 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.
Done!

Testing

Since we have Ant already, testing is a simple affair:
  1. In a temporary directory, create a new build.xml file.
  2. Add the following to the file:
    <?xml version="1.0"?>
    <project name="testcase" default="jython-test">
        <target name="jython-test">
        <script language="jython"> <![CDATA[
    import sys

    print 'Hello, world!'

    def fibo(n):
        a,b = 0,1
        result = []
        while b < n:
          result.append(b)
          a,b = b,a+b
        return result

    print 'Now a quick Fibonacci sequence! ',
    print fibo(100)
    print
    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
    print
    print 'Bye!'
    ]]> </script>
  3. Save it and execute ant in the current directory. You should see something similar to:
    C:\temp\test\> ant
    Searching for build.xml ...
    Buildfile: C:\temp\test\build.xml

    jython-test:
    *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'
    Hello, world!
    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
    reserved.

    Copyright (c) 1995-2000 Corporation for National Research Initiatives.
    All Rights Reserved.

    Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
    All Rights Reserved.

Links

Languages for the Java VM
BSF Project
Python Language Website
Apache Jakarta Ant tool
JPython
Jython
JavaWorld - Script JavaBeans with BSF



© Copyright 1997-2002, R. Lonstein     last modified: 20:22:58 09-Mar-2002