Scripting Jython with JDK 6

Submitted By: Josh Juneau

Introduction

There are many features within the JDK 6 builds which are going to open new doors for Java programmers. Perhaps one of the most unique new features of JDK 6 is the inclusion of a scripting project which allows usage of countless java scripting languages within Java applcations. Sun's implementation of Java SE6 includes a script engine based upon [http://www.mozilla.org/rhino Rhino: JavaScript for Java]. However, this scripting framework has been implemented in such a way that it also supports third-party engines that implement [http://download.java.net/jdk6/docs/api/javax/script/package-summary.html JSR 223 Scripting APIs].

Of course, the topic for this article is the utilization of Jython within a Java application with the new JDK 6 scripting framework. The jython engine is available for download along with the other scripting language implementations on the [https://scripting.dev.java.net/ scripting.java.net website]. While the Jython scripting engine only supports release 2.1, it's utilization provides seamless integration within Java applications...with a minor effort.

Setting Up The Environment

To begin using the JDK 6 scripting engine, you will need to obtain a copy of JDK6. You can use [http://java.sun.com/javase/downloads/ea.jsp this link] to grab the latest release as of this writing. You'll also need to visit the [https://scripting.dev.java.net/ scripting.java.net website] mentioned previously and obtain a copy of the JSR 223 engines.

Once you've installed the JDK and set up the Jython engine, you are ready to begin using the scripting framework. A command line scripting environment is shipped along with JDK 6 and it is useful for testing scripts. However, you can just as easily use the jython interactive command console. Just in case you want to try your hand at using the scripting environment shipped with JDK 6, it is called jrunscript. You will need to use the following syntax in order to invoke a jython command shell using jrunconsole. Execute the following (plugging in your correct paths of course) from the command line:

<<path to jdk6 bin>>\jrunscript -cp <<path to jython jar>>; <<path to jython scripting engine>> -l jython

IDE Recommendation

In order to facilitate the usage of Jython within your Java application, I recommend using an IDE which provides some support for the Jython language. For instance, I use Netbeans along with the [https://coyote.dev.java.net/ Coyote] plugin. You may prefer to use Eclipse or something else as it is personal preference. Personally, I find it cumbersome to go from a Java IDE to a text editor for writing Jython scripts.

Utilizing Jython from Java

As stated previously, hrough the usage of this new JDK 6 scripting engine you can integrete Jython code with Java application code in a seamless manner. I have found some nuances within the documentation for JDK 6 because most of the docs use javascript examples.