The horse starts running (JDK 6 released)!
By sundararajan on Dec 11, 2006
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
What is Scripting API?
Scripting API helps you
- to locate script engine for the language of your choice using
- After locating
javax.script.ScriptEnginefor your scripting language, you just evaluate code in that language by calling, guess what, by calling
evalmethods. 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.
getmethods 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.ScriptContextmay 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
- 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.Invocablefor this purpose as well.
"Hello World" with Scripting
There are two samples in JDK installation:
- $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.
- JavaOne 2006 scripting talk by Mike Grogan and A. Sundararajan
- Sun Tech Days 2006 Scripting presentation by Mike Grogan
- Scripting demo code from Sun Tech Days 2006
- Scripting for the Java platform article by John O'Conner