Scripting Java

The next version of JavaSE 6, codenamed Mustang, will define a framework for scripts and scripting engines to interface and interoperate with Java. Try your hand at scripting using these tips.
Written by Lee Chuk-Munn, Contributor

Script languages have been experiencing a renaissance over the past few years. There are many open-source projects such as Jython, Groovy and Bean Shell to allow the scripting environment to interact with the Java platform. The good news is that the next version of JavaSE 6, codenamed Mustang, will define a framework for a variety of scripts and scripting engines to interface and interoperate with Java.

Through the framework, scripts can create and manipulate Java objects; scripts can also be invoked from within a Java application. Scripting for the Java platform is defined in JSR-223.

The benefits of scripting Java are obvious: First and foremost, the entire Java API are available to the scripting environment. You can create powerful scripts quickly. Secondly, Java applications can be extended by scipts very much like using Visual Basic to add functionalities to Microsoft Office suite of office productivity tools. Finally, we can use the scripting's interactive environments to help us learn about Java APIs.

To try your hand on scripting, you must first download a copy of Mustang. There are two versions of Mustang that you can use: the first is the official beta and the second is the snapshot releases. Download one of these, follow the installation instructions to install it on your machine.

Mustang comes with built-in JavaScript support. There are two ways to access the scripting environment in Mustang:

  1. Using the standalone script interpreter, jrunscript
  2. Programtically invoking scripts from Java applications

Type jrunscriptto enter the scripting shell. Note that jrunscriptwill default to JavaScript if you do not specify it to use other scripting engines.

$ jrunscript
  • Java packages are imported into the JavaScript environment by using the importPackage() function. All Java packages must be prefixed with Packages
js> importPackage(Packages.java.io)
  • Objects are instantiated with the JavaScript new keyword. In the example, we a File object. If you do not have /etc/hosts file on your operating system, you can type in any existent or non-existent file name.
js> var file = new File("/etc/hosts");
  • You can access any static or non-static members of the object. You can also access all the public methods
js> file.separator
js> print(File.pathSeparator)
js> print(file.getName())
js> print(file.canRead())
js> file.getAbsolutePath()
  • You can access JavaBean properties by dropping the 'get'; for example, getAbsolutePath() becomes absolutePath.
js> file.absolutePath
js> print(file.directory)
  • You can look at method, its signature and the method's overload(s), by simply typing in the name of the method like so
js> file.listFiles
java.io.File[] listFiles()
java.io.File[] listFiles(java.io.FilenameFilter)
java.io.File[] listFiles(java.io.FileFilter)
  • Many of the Java APIs event listeners and threads work with interfaces. We can implement Java interfaces in JavaScript by doing the following:
    • Define a Java script object with function and properties whose name match the methods of the Java interface that we are implementing. The Java script object takes the following form {propertyName: value}.
    • Create an object of the Java interface. Pass the JavaScript object that we have created in the constructor
Here is an example of creating a Java thread
js> importPackage(Packages.java.lang)
js> var runit = { run: function() { print("Running... "); } }
js> var run = Runnable(runit)
js> var thread = new Thread(run)
js> thread.start();
js> Running...

Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.

Editorial standards