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!
Scripting API helps you
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
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.
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.
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.
javax.script.Invocable for this purpose as well.
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:
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.