Choosing a Java scripting language: Round two

If you are considering hooking a scripting interpreter into your Java application, the hardest part is choosing which one to use

1 2 3 Page 2
Page 2 of 3

In the previous article, I showed examples for Jacl, Jython, Rhino, and BeanShell. In the interest of brevity, I refer the reader to that article for those source code examples. If you would like to download the examples for all eight of the different languages, please refer to this article's Resources section.

Groovy

To integrate Groovy into your Java application, you create a Binding and instantiate a GroovyShell on that Binding. Then you ask the interpreter to evaluate the source at the filepath you provided on the command line. Here's what the code looks like:

 

import groovy.lang.GroovyShell; import groovy.lang.Binding; import groovy.lang.Closure; import java.io.File;

public class ScriptRunner { public static void main (String[] args) throws Exception {

GroovyShell interp = new GroovyShell(new Binding()); try { File f = new File(args[0]); interp.evaluate(f); } catch(Exception e) { System.out.println("Exception while sourcing file " + args[0]); e.printStackTrace(); } } }

The Groovy script to create a JTree, put it in a JFrame, and show the JFrame looks like this:

 

import javax.swing.JFrame import javax.swing.JTree import javax.swing.WindowConstants

class SimplestGUI { void buildIt() { frame = new JFrame("Simplest GUI"); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); tree = new JTree(); frame.getContentPane().add(tree); frame.pack(); frame.show(); } static void main(args) { b = new SimplestGUI() b.buildIt() } }

JudoScript

To integrate JudoScript into your Java application, you create a JudoEngine and then ask that JudoEngine to evaluate the file at the path specified:

 

import com.judoscript.JudoEngine;

public class ScriptRunner { public static void main (String[] args) throws Exception { JudoEngine je = new JudoEngine(); try { je.runScript(args[0], args, null); } catch(Exception e) { System.out.println("Exception while sourcing file " + args[0]); e.printStackTrace(); } } }

The JudoScript script to create a JTree, put it in a JFrame, and show the JFrame looks like this:

 

const #JFrame = java::javax.swing.JFrame; const #JTree = java::javax.swing.JTree; frame = new java::#JFrame('Simple JudoScript GUI'); tree = new java::#JTree(); frame.getContentPane().add(tree); frame.pack(); frame.setVisible(true);

gui::events { <frame : Window : windowClosing>: exit 0; }

Pnuts

To integrate Pnuts into your Java application, you create a Context, then ask Pnuts to load the file at the path specified into that Context. Here's what the code looks like to make that happen:

 

import pnuts.lang.*; import java.io.*;

public class ScriptRunner { public static void main(String[] args) throws IOException { try{ Context context = new Context(); Pnuts.loadFile(args[0], context); } catch (Exception e) { System.out.println(e); } } }

The Pnuts script to create a JTree, put it in a JFrame, and show the JFrame looks like this:

 

import("javax.swing.JFrame") import("javax.swing.JTree")

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

JRuby

To integrate JRuby into your Java application, you create an instance of Ruby and then ask it to load the file at the path specified:

 

import org.jruby.*; import java.io.*;

public class ScriptRunner { public static void main (String[] args) throws Exception { Ruby runtime = org.jruby.Ruby.getDefaultInstance(); try { File f = new File(args[0]); runtime.loadFile(f, false); } catch(Exception e) { System.out.println("Exception while sourcing file " + args[0]); e.printStackTrace(); } } }

The JRuby script to create a JTree, put it in a JFrame, and show the JFrame looks like this:

 

require 'java' module JavaSwing include_package "javax.swing" include_package "java.awt.event" end

frame = JavaSwing::JFrame.new("Simple Ruby App") tree = JavaSwing::JTree.new() frame.getContentPane().add(tree) frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE) frame.pack() frame.setVisible(true)

Lessons learned

In the trivial example I used, you can see that integration is simple and that the different interpreters complete the tasks similarly. The scripts for the different languages are also similar. I think that you need to look at bigger, more complex examples than we have space for here to see the differences among the interpreters. To pick the best one for your application, you'll have to look deeper at the language syntax and feature set that the scripting interpreters support and decide for yourself if you like what you see.

In the process of doing these integrations, I took notes about what seemed most important, or interesting, or noteworthy about the different interpreters and have listed those notes below. Obviously, these thoughts represent my personal opinion. Software developers are free-thinking people, so I know you'll come to your own conclusions when you do your own due diligence.

Jacl supports the Tcl syntax, which is not difficult to learn. The Tcl programming language is already well known to many software developers, and numerous books and online tutorials are available on Tcl programming. One recommended interactive tutorial on Tcl programming is the TclTutor program, which takes you through the steps of learning to program in Tcl interactively.

JRuby supports the Ruby syntax. The RubyCentral homepage states that Ruby "combines the object-oriented power of the classic OO language Smalltalk with the expressiveness and convenience of a scripting language such as Perl." I wasn't familiar with Ruby, but the developerWorks article "Take a Shine to JRuby" (September 2004) gives you a taste of JRuby's strengths and shows a more elaborate JRuby programming example than I have in this article.

BeanShell 2.0 release notes state that BeanShell is now capable of interpreting ordinary Java source files, which is impressive. I tested this functionality and found it to work fine for the simple programs I asked it to load and run. If you want to learn more about BeanShell programming, check out the BeanShell tutorial on the BeanShell Website.

Jython supports the Python syntax. If you are unfamiliar with Python and don't have one of the Jython books handy, one place to start learning about Python is with Guido van Rossum's Python tutorial. One feature of the Python syntax is that it doesn't use braces to group statements together, it uses a combination of the colon character (:) and space indentation. This might seem like it could lead to confusion in the code, but consistent usage of spaces instead of tabs is all that's required to keep things clear. If you are looking for in-depth programming advice on Jython, several good books are available.

Rhino supports the JavaScript syntax, which is straightforward and very readable. This language has been well documented and is already known to most Web developers. That might be all the reason you need to make this the obvious choice for you. The documentation is well done. The presence of a debugger is a strong selling point for those writing complex or lengthy scripts. The Mozilla Website has a tutorial on embedding Rhino.

JudoScript syntax is easy to pick up from the documentation and seems to include everything you need to get a complicated programming job done: threads, function pointers, and exception-handling are all supported and well-explained. To get a sense of JudoScript's strengths, read the JudoScript White Paper. For more information, the JudoScript Website offers tutorials on the JudoScript language and on how to embed JudoScript in a Java application.

Groovy syntax is Java-like and readable. One interesting feature of Groovy is that it supports closures, which let you define a piece of programming code without declaring a class or a method. You can assign that piece of programming code (the closure) to a variable if you want, pass that variable through other functions, and call that closure whenever you need by just executing the call() function against it. As I mentioned earlier, there is a JSR committee working on the language specification for Groovy, which is good. One caveat is that the Groovy syntax is still evolving, so there is a chance that Groovy scripts written today might need to be rewritten when the committee nails down the final syntax. If you are interested in reading more about the language, several good articles cover the topic, including "Groovy, Java's New Scripting Language" (O'Reilly, September 2004) and "Groovy, Scripting for Java" (Object Computing, 2003).

The only hiccup I found with the Groovy interpreter occurred when I broke an assignment statement between two lines as shown below. In this case, the interpreter seemed to ignore the part of the expression that incremented the variable by 1, like this:

    i = i
       + 1;

But doing things the following way worked just fine:

    i = i +
    1;

I was running a beta version of 1.0, which means the parser in that build isn't totally bulletproof. It's beta, right? Groovy is getting a lot of development attention and no doubt this glitch will be addressed in a future build.

Pnuts syntax also looks similar to Java. One interesting and useful feature of the Pnuts syntax is the module concept. Reusable scripts in Pnuts can be divided into modules, which are similar to Java packages. Those modules, once loaded, can have naming conflicts or collisions resolved in a script by adding the use(module name) call, which tells the interpreter which module takes precedence over other modules for finding a binding for a variable or function name. The Pnuts language syntax seems easy to pick up from the documentation, and the debugger is useful.

If you are looking for more information comparing the programming interfaces of the interpreters, you might look at "Embedding APIs of Java-Based Scripting Engines" which compares the Jacl, BeanShell, Jython, Rhino, Groovy, and Pnuts interpreters performing common tasks such as evaluating an expression from a string, catching an exception thrown by a script, or setting or getting the value of a scripting variable.

The third benchmark: Licensing

Although these interpreters are easy to download and play with, what do their licensing rules say about you embedding it in a commercial application that you sell to customers?

The answer is that licensing is not a problem for any of these libraries. The way I read the license agreements, in each case, the user must abide by the GNU Lesser General Public license or an equivalent. This means that 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 may have to clarify to users that the rights to the scripting interpreter bundled with your application belong to someone else. Of course, if you're embedding the interpreter in a commercial application, the smart thing is for you and your company attorney to carefully look at the licensing agreement.

Final thoughts

If you need to integrate scripting support code into your Java application, my advice is to pick a single scripting interpreter and standardize on it. Costs are associated with each scripting language you support in your product, so don't make more work for yourself by trying to hook more than one scripting interpreter into your application. When adding scripting support, you can further simplify things by using an interpreter written in Java instead of a native interpreter such as Python or Tcl. That will make your solution more portable and simplify the integration task between your Java program and the interpreter.

If your developers or customers are already familiar with a particular scripting language like Tcl, Python, Ruby, or JavaScript, obviously you'll want to look seriously at the interpreter that supports that language (Jacl, Jython, JRuby, or Rhino, respectively). If you don't have that constraint, you will have a harder choice. In some ways, it's a bit like going to a new car lot. All of the choices will work, so you are left with balancing the differences between the alternatives, such as performance or options.

Some of these interpreters perform simple tasks faster than others. Some are updated and released more often, or have better documentation or debugging facilities than others. Some support compilation of scripts to bytecode. Some have language syntaxes that will either appeal to a developer or not, depending on preference, programming background, and the specific task at hand. As with most engineering tasks, you have to define your requirements and then investigate some to come up with the right answer.

If I had to distill what I learned from working with the different interpreters down to a bare minimum, here's what I'd say:

Jacl eases your entrance into scripting. Integration is simple, and if you need your scripts to be written in Tcl, it works well. If speed is your top priority, you may want to consider other choices.

Jython is one of the fastest scripting interpreters. From looking at the Website, it seems that Jython development is about to renew, which is good news. There are several good books on Jython. If you like the Python language, Jython is a solid choice.

1 2 3 Page 2
Page 2 of 3