Tips for Scripting Java with Jython

Maybe you're writing a Java program, and you need to do a quick one-off script to clean up your database. Sure, you could write it in Java, but that can involve a lot of overhead for a quick script. You could write it in an outside scripting language, but you'd really like access to the business logic already in your Java program.

Or maybe you’re writing a servlet, and the requirements are changing quickly. Your data model is changing, and you're having trouble keeping all the type information consistent.

Or maybe you’re writing a standalone program, and you'd like to use the large variety of tools already written for Java, but you'd also like to use a tool that makes your program 25-50 percent shorter, and easier to write and maintain.

Jython is the tool for you.

Jython is a complete re-implementation of the Python programming language, written in 100 percent pure Java, which runs on any Java Virtual Machine (JVM). Python is a high-level, interpreted, object-oriented scripting language which combines elegant syntax rules with powerful built-in objects to create a language that is very easy to write, read, and maintain. Python is perhaps most notorious for its use of indentation to mark the beginning and end of statement blocks.

There are usually two reactions to this description of Jython. Some people ask, "Does it really work?"

It really works. Anything you can do with the Java SDK can be done in Jython, including standalone applications, applets, servlets, and beans. Arbitrary Java code can be called from Jython, Java objects can be used as-is, or can be subclassed in Jython. You can even write an object in Jython that can be used or subclassed by other Java code. In addition, nearly all of the modules of the standard Python library are available to be used from Jython.

Another common reaction is to ask why Jython is needed, since Java already exists. Java, like every programming language, is a tool, and it has its uses. But it can't be all things to all people. Outside of the Java world, you can use C/C++, Python, Perl, or any of dozens of other languages, each with its own strengths and weaknesses. Jython gives some of the same flexibility to the Java programmer.

Here are 11 specific features of Jython that can be particularly time-saving or exciting for Java programmers.

1. Interactive Interpreter

The Jython interpreter can run in an interactive mode. In this mode you can type in Jython code one line at a time and see the results. You can even interact with Java libraries via this interpreter. For example, if you type the following at the Jython prompt:

Jython will create a swing JFrame and display it on screen. You can continue to view and modify the attributes of the JFrame via the prompt. This can be tremendously useful during prototyping or while debugging. (You've probably noticed what seem to be some type inconsistencies in the third line of that code -- for more on that see tip #9).

2. Built-in Data Structures

The Jython analogues to Java's collection classes are much more tightly integrated into the core language, allowing for more concise descriptions and useful functionality. For example, notice the difference between the Java code:

Jython's for statement is dependent on the list structures, so if you wanted to iterate over the above list, you would write it:

for i in list:

Which is just a bit simpler than the Java idiom for iterating over a list:

for (Iterator i; i.hasNext();) {
i.next();
}

In Jython, the for statement will also automatically iterate over Java Lists, Arrays, Iterators, and Enumerators.

3. List Comprehensions

Jython offers list comprehensions, which are a cool shortcut for the common task of creating a list by applying a specific function to each element of an existing list. For example, in Jython you can write:

4. Dynamic Variable Creation

In Jython, variables do not have to be declared before use, nor do their types need to be declared. (They do, however, have to be assigned before use). This is true both for local variables within a block, and for the data members of classes. Jython is, however, still strongly typed, and like Java, will not automatically cast variable types. Attempting to do something like:

1 + "1"

will raise an exception in Jython at runtime.

If you have never programmed in a dynamic language, it is understandable that the idea of not declaring variables or types may make you a little nervous. Programmers who switch to a dynamic language are often surprised by how rarely type errors actually occur in practice, and how quickly they are found and fixed when they do occur. When compared with the amount of effort spent in Java convincing the compiler that your program is legal, you may find that static typing may not always be worth the effort.