ava Specification Request (JSR) 223
defines a set of APIs and a related framework to integrate the Java platform with scripting languages. The APIs are part of the standard library that ships with Java SE 6
, so you get free scripting support whenever you run applications on a SE 6 JVM. This also applies to applications built upon the Eclipse platform.
JSR-223 defines various kinds of interactions between scripting languages and the Java platform, including:
- Embedding interpreted scripts into Java applications
- Modifying and controlling Java objects from within a scripting context
- Using the Java language to write and expose script interpreters
This article demonstrates how to enhance the Eclipse platform and the applications built upon it with the power and benefits of scripting languages, taking advantage of these new Java SE 6 capabilities. You will learn how to extend the Eclipse Platform with scripts to enable the following:
- Automate repetitive tasks you usually perform within your integrated development environment
- Perform fast user interface (UI) prototyping by modifying both the UI and the control logic on the fly
- Allow users to customize your applications beyond the common sets of preferences, enabling users to add small snippets of logic into their preferred scripting or domain specific languages (DSLs).
Most of these benefits derive from the nature of scripting languages. They generally are dynamically typed, in some cases are specific to a particular domain of problems, usually are simpler to learn and write than compiled languages, and not bound to a write-compile-run cycle like older languages such as Java and C.
What You Need
You need only a basic understanding of JSR-223 internals to follow this article. In fact, it is sufficient to understand only the following piece of code:
Map<String,Object> vars =
getScriptVariables(); // fictional method
String scriptBody = getScriptBody(); // fictional method
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByExtension("js");
for (String key : vars.keySet())
// ScriptException handling code omitted
The code basically does three things:
- It creates a javax.script.ScriptEngineManager (searching it by supported extensions), which is responsible for the discovery and instantiation of scripting engines. (Engines are the components that evaluate and effectively run scripts.)
- It sets a bunch of script variables with the ScriptEngine.put(String,Object) method, thereby defining a binding between a Java object and the scripting environment so the script will be able to manipulate such objects. More generally, the script can control the Java environment.
- It evaluates a given script with the eval(String script) method.
The JVM discovers available engines using the Service Provider mechanism, which involves scanning for particular configuration files in the META-INF/services directory of the jar files available to the application. To make a scripting engine available within your application, you need only add to the classpath a correctly configured jar file containing that script engine. This article uses some of the ones provided by the scripting project at dev.java.net, such as the Ruby and Groovy engines.
This is an overly reductive use of the scripting APIs to be sure, but it serves the tutorial purposes of this article. (Refer to the Related Resources section in the left-hand column for a link to more detailed documentation.)