|
There have been many tools developed to allow you to add scripting capabilities
to your java code. If you're wanting to compare the available options, a good
place to start is with David Kearns' article titled Java
scripting languages: Which is right for you? which was published in JavaWorld in April 2002.
Below I detail some of the java scripting tools and technologies that I have
explored.
|
|
BeanShell
BeanShell is a small, open source, free
Java source interpreter. Embed the BeanShell intepreter into your Java
application, and you have a scripting API through which you can:
- Access and modify any object in your JVM
- Instantiate new objects in your JVM
- Dynamically declare and instantiate new classes
And all this can be done with familiar Java syntax (plus the some convenient
scripting macros).
I have used BeanShell as an interactive debugging tool (quite possibly
THE most useful debugging tool I have come across, I might add), and to provide
a scripting API to a task sensitive Graphical User Interface.
In May 2005, JSR 274 was
announced as a means to standardize the BeanShell scripting language. The goal
of this JSR will be to formalize the langauge and provide the RI and TCK which
will allow it to be included in a future release of the J2SE. This effort will
build upon the introduction of the javax.script API (see JSR 223) by providing a
standard, Java syntax compatible scripting language as part of the Java
platform.
|
|
Bean Scripting Framework
The Bean Scripting
Framework (BSF) is an attempt by IBM to define and develop a generic
architecture that allows scripting to be added in a simple and uniform way to
Java applications. Using the BSF architecture, it should be possible to allow
an application to be scripted from any BSF supported language, without
any particular scripting language dependencies.
As a concrete examples, this should allow you to start using Jython as the
scripting API for your application, then seamlessly (with no code changes)
change to using BeanShell instead.
|
|
JSR223: Scripting for the Java Platform
The BSF has been largely superceded by the "Scripting for the Java
Platoform" JSR (JSR-233)
This JSR has defined a new API for exposing scripting language engines to the
Java platform. The new javax.script package provides a standardized script
engine API, similar to the IBM/Apache BSF, but takes the integration much
further, offering: a more powerful discovery mechanism, fine grained namespace
and script environment management, powerful application integration, and other
advanced features.
The Java Scripting API includes:
- Standardized packaging and deployment of scripting language engines for use
with Java applications and web apps. The JAR services mechanism allows support
for new languages to be added by simply dropping in a new jar file.
- An API for discovery and instantiation of scripting language engines
utilizing metadata, including: common language names, file extensions, and
MIME types.
- A simple API for evaluation of scripts, optionally supporting script
compilation and method invocation for both procedural and object oriented
scripting languages.
- A fine grained, pluggable namespace and script context API, allowing
complete control over the evaluation environment of scripts and management of
engines. This allows developers to create their own namespaces of bound values
with well defined scoping relationships.
- Tight application integration via Map based namespaces and proxy
Java interface binding to scripts. This will allow developers to expose parts
of the host application to scripts via a Map interface that binds directly to
the namespace of a script, as well as to expose methods of a procedural script
via a real Java proxy interface.
- A standardized classification of script engine threading models, allowing
developers to optimize engine usage if desired. Languages are categorized
into one of three levels of concurrency support: Java language semantics,
"thread isolated" concurrency, and purely stateless concurrent
interpreters.
- A deliberately limited, but useful API for language independent script
code generation. This allows clients to generate basic "output" and method
call statements in a language neutral way, enabling simple macro generation
and scripted action recording in any target language.
- A "non-normative" description of Java bindings which can be implemented by
languages that to work with Java methods and objects. This includes a
reference implementation intended to clarify and ease the implementation of
finer points of Java language semantics such as overloaded method
resolution.
Further doucmentation and the reference implementation is available from the
JSR-223
website.
|
|
Jython
Jython (formerly JPython) is an
implementation of Python written entirely
in Java, which allows you to run Python on any Java platform.
The secret to Jython's popularity lies in the combination of Java's libraries
and tools with Python's rapid development capabilities.
If you're interested in using Jython, then you may be interested in the Jython Essentials book
from O'Reilly.
|
|
Rhino
Rhino is an open-source
implementation of Javascript written entirely in Java. Rhino currently
implements JavaScript 1.5, which itself conforms to Edition 3 of the ECMA-262 ECMAScript Standard.
|
|
Tcl/Java
Tcl/Java consists of two
pieces of software:
- Jacl (Java Command Language) is an
implementation of a Tcl 8.x
interpreter, written entirely in Java.
- Tcl Blend is a Tcl 8.x
package extension that allows you to load and interact with the Java Virtual
Machine from within Tcl.
Ray Johnson, Project Lead for the 1.0 release of Tcl/Java at Sun Labs, wrote a
white paper about the motives and goals for the integration of Tcl and Java.
|
|
Swank
SWANK is a companion graphical user
interface toolkit for Tcl/Java. Jacl/SWANK forms an analagous pairing to Tcl/Tk and can be used to rapidly
script user interfaces.
|
|
Java Expression Parser
Java Expression Parser(JEP) is a Java
API for parsing and evaluating mathematical expressions. With this library you
can allow users to enter an arbitrary formula as a string, and instantly
evaluate it. JEP supports user defined variables, constants, and functions. A
number of common mathematical functions and constants are included.
|
|
JudoScript
Judoscript is an open source java
scripting language which is close in syntax to the forth-coming ECMAScript
Edition 4.
Judoscript claims to be the only language that support scripting at the Java
object level, OS level and domain level. It's a little unclear to me exactly
what this claim means, but you can read more about JudoScript in this JudoScript
WhitePaper.
|
|
JRuby
JRuby is a Java implementation of
the Ruby interpreter. JRuby is tightly
integrated with Java to allow both to script any Java class and to embed the
interpreter into any Java application.
|
|
Groovy
Groovy is a dynamic scripting
language that uses a Java-like syntax. It aims to be an easy-to-learn scripting
solution for Java programmers. Not having used Groovy, it seems similar to
BeanShell in a lot of ways. It is currently going through the JCP (as JSR 241), meaning that one
day, it will hopefully appear as part of the standard J2SE. This would give it
an inherent advantage over other scripting languages for the JVM.
|
|
Sleep
Sleep is an embeddable scripting solution
for Java applications. Sleep is heavily inspired by Perl with bits of
Objective-C thrown in. The Sleep Java API allows the language to be extended
with new constructs, operators, functions, and variable containers.
The Sleep library provides a parser, interpreter, and default library.
Embedding Sleep consists of extending the language to include functions,
variables, and other features to access an application's functionality.
Scripters can then use these additions to manipulate the application from a
script.
Read More about Sleep:
|
|