The horse starts running (JDK 6 released)!

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.

References

Comments:

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.

Posted by scriptics on December 14, 2006 at 04:45 AM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder