RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Java SE 6's New Scripting and Compiling Goodies : Page 2

Among the most intriguing features of the first Java SE 6 release candidate are the capabilities the new scripting and compiling APIs deliver.

The Java Scripting Engine in Action
The Java SE 6 installation comes with sample applications to demonstrate many of the release's new features. One of these samples, ScriptPad, is a 99 percent JavaScript application that loads and executes other scripts, all through the Java scripting API. It uses Java to load the Rhino JavaScript engine and execute the five script files that are built into ScriptPad. Included in the sample/scripting/scriptpad folder within your Java SE 6 installation root folder, ScriptPad comes complete with a NetBeans 5.5 project file, which you can use to easily build and execute it. (Figure 1 shows the main window of a ScriptPad application.)

You can also use the application (through the Java Scripting API) to run other scripts that you've written. For instance, the script shown within the ScriptPad window in Figure 1 accesses Java's static System object, invokes its method (getProperties), uses the resulting Properties object to get the name of the host operating system, and outputs the result in an alert window.

Figure 1. The Main Window of the ScriptPad Application

When you execute this script on a Windows XP machine, you get the output shown in Figure 2.

Figure 2. Output of ScriptPad Script on Windows XP

This is only a simple example of what you can do when you combine scripting and Java. However, it does illustrate how seamless the integration really is. You can execute any script entered in ScriptPad with the Tools..Run menu option.

The following Java code loads the Rhino JavaScript engine, loads the script contained within the file browse.js, and executes the function named browse (This script was adapted from a sample that is distributed with Java SE 6):

import java.util.*;
import java.io.*;
import javax.script.*;

public class Main
    public Main()
        try {
            ScriptEngineManager m = new ScriptEngineManager();
            ScriptEngine engine = m.getEngineByName("javascript");
            if ( engine != null )
                InputStream is = this.getClass().getResourceAsStream("browse.js");
                Reader reader = new InputStreamReader(is);
                Invocable invocableEngine = (Invocable)engine;
        catch ( Exception e ) {
    public static void main(String[] args)
        Main m = new Main();

The browse.js script itself (see Listing 1) uses the new desktop features of Java SE 6 to open the default browser on the host OS to the page http://www.ericbruno.com.

This paradoxical example where Java invokes a script that in turn invokes Java to load an HTML page demonstrates just how dynamic the scripting API can be.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date