Sundararajan's Weblog

  • Java
    December 11, 2006

The horse starts running (JDK 6 released)!

Guest Author

Yes, JDK 6 has been released today. As many of you know already, scripting is one of the important features of JDK 6. Scripting API is in the javax.script package which is specified by JSR-223. It is very simple API to use scripting languages from Java code. To use scripting language from your Java code, you need to have JSR-223 compliant "script engine" - i.e., implementation of the javax.script API for your language of choice. Sun's implementation of JDK 6 comes with JavaScript script engine - which is based on Mozilla's Rhino implementation. So, you can "eval" JavaScript code from your Java code!

What is Scripting API?

Scripting API helps you

  • to locate script engine for the language of your choice using javax.script.ScriptEngineManager. Please note that to use any language other than JavaScript [which is bundled with JDK 6], you need script engine implementation for your language. You can download jsr-223 script engine most popular scripting languages such as Groovy, JRuby and many other languages from scripting.dev.java.net
  • After locating javax.script.ScriptEngine for your scripting language,
    you just evaluate code in that language by calling, guess what, by calling eval methods. You can evaluate script code from a String or from a java.io.Reader.
  • In addition to evaluating script code from Java code, you may want to "connect" script to the Java objects of your application. javax.script.ScriptEngine has put and get methods to expose Java objects as "global" variables to script. In addition, there are interfaces such as javax.script.Bindings [Bindings is "scope" - a set of name, value pairs] and javax.script.ScriptContext may be used for finer control. The later is used to support one or more scopes in the script global namespace.
  • Sometimes you may want to call a specific script function - rather than evaluating script again and again. If your script engine supports javax.script.Invocable [yes, the bundled JavaScript engine and most engines at scripting.dev.java.net support this optional interface], you can use it to call a specific script function from Java code. This may be used to call script function, say for example, from a user interface event handler.
  • It is also possible to implement a Java interface by script functions [rather than writing Java code for interface impl.]. Again, you have to use javax.script.Invocable for this purpose as well.

"Hello World" with Scripting

Simple main class to evaluate JavaScript to print "hello world":

import javax.script.\*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");
jsEngine.eval("println('hello world')");

There are two samples in JDK installation:
  • $JDK/sample/scripting/scriptpad/ is a simple Notepad-like editor that allows editing/running JavaScript code - this is mostly written in JavaScript with few lines of Java code. More details here.
  • $JDK/demo/scripting/jconsole-plugin is a plugin to jconsole (Monitoring and management client tool shipped with JDK). More details here.

If you are interested in using scripting on the serverside code, you may want to look at the Phobos project. This project uses JSR-223 scripting feature.


Join the discussion

Comments ( 1 )
  • scriptics Wednesday, December 13, 2006
    The jrunscript utility distriuted with JDK 1.6 and the ScriptEngineMnager force the ScriptEngine for a language to accept SimpleBindings in the setBindings method.
    This seems to be at variance with the basic design in which Bindings is just an interface and the language is free to implement it any way convenient.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.