ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

The Mustang Meets the Rhino: Scripting in Java 6

by John Ferguson Smart
04/26/2006

The latest major Java release (Java SE 6, aka Mustang), is now in its beta version. Although this new version is not as major an update as Java 5, it does come with a few interesting new features. Undoubtedly, one of these is its support for scripting languages.

Scripting languages such as PHP, Ruby, JavaScript, Python (or Jython), and the like are widely used in many domains, and are popular because of their flexibility and simplicity. Scripts are interpreted, not compiled, so they can be easily run and tested from the command line. This tightens the coding/testing cycle, and increases developer productivity. They are generally dynamically typed, and have expressive syntaxes that allow an equivalent algorithm to be written much more concisely than in Java. They are also often fun to work with.

Using scripting languages from Java can be useful in many situations, such as providing extensions to your Java application so that users can write their own scripts to extend or customize the core functionalities. Scripting languages are both simpler to understand and easier to write, so they can be ideal to give (technical) end users the possibility to tailor your product to their needs.

Many independent scripting packages have been available for Java for some time, including Rhino, Jacl, Jython, BeanShell, JRuby, and others. The novelty is that Java 6 provides built-in support for scripting languages via a standard interface.

Java 6 provides integrated support of the JSR-223 specification. This spec provides a convenient, standard way to execute scripting languages from within Java and provides access to Java resources and classes from within scripts. Java 6 comes with a built-in integration of Mozilla's Rhino implementation of JavaScript. And based on this spec, support for other scripting languages such as PHP, Groovy, and BeanShell, is in the pipelines. Rhino implementation is the focus for this article, but other languages should be essentially the same.

Where do scripting language names come from? Well, since most scripting languages are generated from open source projects, the names generally are the product of the imagination of their respective authors. Rhino gets its name from the animal on the cover of the O'Reilly book about JavaScript. PHP, in the self-referential Unix tradition, is short for PHP: Hypertext Preprocessor. Jython is a Java implementation of the Python scripting language. And Groovy just seemed like a cool name.

Using the Scripting Engine

The JSR 223 specifications are convenient and easy to use. To work with scripts, you only need to know a couple of key classes. The main one is the ScriptEngine class, which handles script interpreting and evaluation. To instantiate a script engine, you use the ScriptEngineManager class to retrieve a ScriptEngine object for the scripting language you're interested in. Each scripting language has a name. The Mozilla Rhino ECMAScript scripting language (commonly known as JavaScript) is identified by "js".



        ScriptEngineManager manager
            = new ScriptEngineManager();
        ScriptEngine engine
            = manager.getEngineByName("js");

Embedded JavaScript can be used for a variety of purposes. Since it is more flexible and easier to configure than hard-coded Java, it can often be used to code business rules that may change often. Scripting expressions are evaluated using the eval() method. Any variables used in the scripting environment can be assigned from within your Java code using the put() method.



        ScriptEngineManager manager
            = new ScriptEngineManager();
        ScriptEngine engine
            = manager.getEngineByName("js");
        engine.put("age", 21);
        engine.eval(
            "if (age >= 18){ " +
                        "    print('Old enough to vote!'); " +
                        "} else {" +
                        "    print ('Back to school!');" +
                        "}");

        > Old enough to vote!

The eval() method also accepts a Reader object, which makes it easy to store scripts in files or other external sources, as in the following example:


        ScriptEngineManager manager
            = new ScriptEngineManager();
        ScriptEngine engine
            = manager.getEngineByName("js");
        engine.put("age", 21);
        engine.eval(new FileReader("c:/voting.js"));

Pages: 1, 2, 3, 4

Next Pagearrow