Java scripting languages: Which is right for you?

When hooking scripting language support into a Java app, choose wisely

1 2 Page 2
Page 2 of 2

BeanShell

BeanShell integration with your application is simple, like the rest. Add the BeanShell jar file to your classpath at invocation, then instantiate the interpreter prior to executing a script:

import bsh.Interpreter;
public class SimpleEmbedded {
    public static void main(String []args) throws bsh.EvalError {
       Interpreter i = new Interpreter();
    }
 }

The BeanShell script to create a JTree, put it in a JFrame, and size and show the JFrame is simple and Java-like:

frame = new JFrame();
tree = new JTree();
frame.getContentPane().add(tree);
frame.pack();
frame.show();

What these integration examples mean

The information above shows that it's easy to integrate any of the interpreters. And once you understand the correct syntax from your scripts, you can be productive writing scripts, too. For the trivial example I used, you can see that scripts written in BeanShell and JavaScript are the most Java-like in their formatting, with Jacl and Jython showing some differences, but not proving unreadable. As the scripts for each interpreter show, no firewall exists between the script code and classes defined in your application's Java libraries. So take note: the script code seamlessly runs against your application classes, so be sure that's what you want. If you want to protect parts of your application from scripts at runtime, take steps such as obfuscating classes not part of your published API to prevent people from coding scripts against them.

The fourth benchmark: Library support issues

Though scripting support gives your application extra power, it requires your application to depend on those scripting libraries. Before you tie yourself to a scripting interpreter, consider the chance you someday may own the code you are integrating against.

If the support team seldom updates and releases the interpreter, that's a bad sign. It either means the code is perfect as it stands, or the developers who took care of the code have moved on to other projects. Guess which case is more likely?

It is also worth checking to see how much source code it took to implement the interpreter. It's unrealistic to think that you can understand an interpreter's every code line and extend it or tune it to fit your needs. The interpreters are just too big. Still, it's worth knowing these interpreters' sizes. At some point you may need to modify the interpreter source code or dig through the code to figure out the interpreter's behavior.

Let's go through the library support issues for each interpreter in our survey.

Jacl

Jacl has an active support group. Although the download link at the development site points you at a release that is several years old, newer versions can be found using the CVS version control system. Jacl consists of about 37,000 lines of Java code.

Jython

Jython seems to feature active support, maintenance, and updates. Jython consists of about 55,000 lines of Java code.

Rhino

Rhino receives updates and new releases on a frequent basis, and comprises about 44,000 lines of Java code.

BeanShell

BeanShell features regular updates and releases; it consists of about 25,000 lines of Java code plus a substantial number of BeanShell scripts.

What the support information means

All four interpreters are big, complex beasts. You'll be ahead in the game if you can rely on a support team to make improvements and fix bugs. Before you choose an interpreter, check to see how often it is updated and released. You might contact one of the developers to see what the long term plans for the interpreter are and to see what their process is for fixing bugs.

Licensing

Even though you can download these interpreters for free, what are the licensing rules about embedding the scripting interpreter in a commercial application?

Fortunately, licensing does not present a problem for any of these libraries. The way I read the license agreements for Jacl, Jython, JavaScript, and BeanShell, the user must abide by the GNU Lesser General Public license or an equivalent. That means you can ship the libraries with your application even if your application is not free. However, you cannot strip the copyright headers out of their source files or script files, and you may need to clarify to users that the rights to the scripting interpreter bundled with your application belong to someone else. Naturally, don't take my word for it. When you get interpreter distribution you care about, read and interpret the licensing information for yourself, and have your legal department affirm you can use it with your application without creating any problems for you.

Final thoughts

If you need to integrate scripting support code into your Java application, I advise you to standardize on a single scripting interpreter. Each additional scripting language you support in your product has associated costs, so avoid hooking more than one scripting interpreter into your application. When adding scripting support, you can simplify things further by using a Java-based interpreter instead of a native interpreter such as Perl. That will make your solution more portable and simplify the integration task between your Java program and the interpreter.

If your customers expect a particular language to customize your products, look seriously at integrating with a scripting interpreter that supports that language, in the way that Jacl supports Tcl syntax. If you are not locked to a particular language, compare the interpreters from a variety of standpoints to see which are better suited to particular tasks than others.

For instance, Jacl moves extremely slowly compared to the other interpreters, but proves useful if you need your scripts written in Tcl. If you're porting an application from Tcl/Tk to Java, the ability to run your old Tcl scripts in the new Java application might be so valuable that it would outweigh other concerns. Also, Tcl is a popular programming language, so many developers are already familiar with it, and Tcl books are easy to find.

If you want Java-like scripting code and prefer painless integration, BeanShell looks good. On the downside, the user guide for BeanShell syntax and programming is limited to what ships with the release, and it runs slower than some of the other interpreters. On the other hand, I found BeanShell easy to work with and liked it quite a bit. Its well organized libraries make integration simple. If performance is not the most important criteria for your scripting interpreter, consider BeanShell.

Rhino runs significantly faster than BeanShell and likewise supports Java-like syntax in its scripting. Moreover, it appears well written and well supported, and you'll find numerous JavaScript syntax books at the bookstore. I had no problems and recommend it if you have equally balanced needs for performance, Java-like syntax, and strong support for the libraries.

Jython is the fastest scripting interpreters I looked at and has some powerful programming features. My only real concern was about Jython's control-flow syntax, which might or might not be important to you. As with Jacl, writing scripts for Jython may have more of a learning curve to it than JavaScript or BeanShell do, since there is more new ground to cover. If you want to write nontrivial scripts in Python, I recommend buying a book. Python is a popular programming language, so you'll have plenty to choose from at the bookstore.

Many thanks to Geetha Pannala and Blair West of Mentor Graphics for their assistance in looking at the different scripting interpreters.

David Kearns is a Sun Certified Java 2 Programmer and Developer. He possesses 13-years experience designing and developing electronic design automation (EDA) tools and object-oriented application frameworks in C++ and Java. For the last 5 years, he has worked on commercial Java applications for configuration management, process control, tool scripting, and design simulation waveform viewing. David works at Mentor Graphics in Wilsonville, Ore. as a staff engineer. His current project at Mentor Graphics is developing a high-performance Java waveform viewer.

Learn more about this topic

1 2 Page 2
Page 2 of 2