ava inherited many of its defining characteristics from C and C++, making it substantially easier to work with. Unfortunately, despite these many syntax improvements, Java still has to be compiled.
Of course, to compile source code requires that a framework for executing a program be constructed. In Java that means you have to write both a class file and a main entry point method. While this isn't necessarily much of an issue when working with familiar APIs, it can slow development down if you're working in unfamiliar territory. Fortunately, there are an increasing number of options available to Java developers to test code and circumvent the compile step. Jython is one such tool, and the subject of this article.
Jython is the Java implementation of the Python programming language. (For more information on Python and Jython see the Related Resources section in the left column.) Using Jython you can write and test Java code without running 'javac.' Once you see how easy this technique is to use, you'll realize what a time saver Jython can be.
This article will focus on using Jython from the command line interactive interpreter, but there are many reasons to use Jython for other programming tasks.
- Jython, like Python, is stable, robust, and free.
- Jython/Python syntax is clean and simple. Typically, you can write a Jython/Python routine with just a fraction of the code required in Java.
- Due to its brevity, Jython/Python is a great language for building prototypes.
- Jython/Python has numerous libraries that support almost every conceivable task you'd like to perform.
- While you can write a simple script, Jython/Python is a dyed-in-the-wool object-oriented language.
- A Jython interpreter can be embedded in an application to allow users to script dynamic behaviors.
- You can write Jython scripts that incorporate Java classes and run them by calling 'jython [script name]'.
- A Jython script can be compiled down to Java bytecode and executed just like any Java program.
- Jython/Python code resources are plentiful on-line.
If there is a downside to Jython, it is that it hasn't kept up with the C Python implementation and isn't compliant with Java 1.5. Happily, there is a proposal
to remedy this problem and according to the published schedule, work is underway. In the mean time, let's take look at how you can use Jython to exercise Java APIs.
After downloading Jython
and installing it in the directory of your choice, you first need to start up the interpreter by opening a DOS or shell window and navigating to the root of your Jython installation (see Figure 1
). On Windows there is a jython.bat
file that you execute to start up the interpreter. The batch file is simply a wrapper call to use Java to start up Jython (remember it is really just a Java program) and pass along some settings for the classpath and a system property.
|Figure 1. Finding the Root: Here's what it looks like to launch Jython from a command prompt. |