Monday, March 9, 2009

JSR 223: Scripting for the Java Platform

We have all heard heated arguments between developers who use scripting languages and developers who use Java. One of the reasons for the war between these two factions is that the process of integrating the two was so difficult that developers on both sides were almost forced to choose one or the other. Java Specification Request (JSR) 223 addresses this issue. In this article, I present examples of how the standard framework and API defined by JSR 223 makes it easy for Java programmers to take advantage of the benefits of using a scripting language while retaining the benefits of using Java.


The Scripting API

The entire scripting API is contained in the package javax.script, which was first delivered with Java 6. This lightweight package contains six interfaces:


Bindings
Compilable
Invocable
ScriptContext
ScriptEngine
ScriptEngineFactory

five classes:


AbstractScriptEngine
CompiledScript
ScriptEnginemanager
SimpleBindings
SimpleScriptContext <.ul>

and a single exception:


ScriptException

Rather than describing each of these interfaces, classes and exceptions, I present a series of five Java programs and five scripts in three languages to show just how little effort is required to make them work together.

No comments: