Categories
Tags
Bloggers
andreaprovaglio.com

Blog @ andreaprovaglio.com

Thoughts on software development, design and people.

Jan 09, 2008

Using JSR-223 on Java 1.5 and 1.4

I find the Scriping for the Java Platform API (JSR-223) an interesting and promising extension to Java. It's even more appealing to me when used with JRuby (and optionally with Rails). I'll discuss the benefits of such a mix in a future post. For now, I just want to talk about the Java versions on which you can run JSR-223.

Although Sun is promoting JSR-223 as a Java6 feature, you can in fact run it on previous versions as well. This is good news for those of us who can't upgrade their servers to a major release so easily - and for those who, like myself, use Mac OS X and are still waiting at the time of this writing for Apple to release a stable Java6 update.

All you have to do to run JSR-223 on a Java version preceding Java6 (I tried with Java5) is:

  1. Download the reference implementation of the Java Scripting API
  2. Check-out from the CVS the source code for the scriping engines (NOTE: the build requirements ask for Java6, but I compiled just fine on Java5)
  3. Recompile the engine for your scripting language(s) of choice with the version of Java you want to use
  4. Add to your classpath the required jars (more about this below).

One tricky bit about recompiling the scripting engines is that you'll need to specify in an Ant property the path to the script-api.jar from the reference implementation, and on Mac OS X that path must be absolute. Running the Ant script will tell you how to provide that property.

To keep things simple I decided to recompile only the engine I needed (I wanted to use JRuby, remember?), so I commented out from the Ant build script the tasks for all the languages that I didn't need. Eventually, I ended up with a shiny jruby-engine.jar file.

At this point, to be able to compile and run a small Java program which loads and runs a Ruby script, all I had to do was to include in my Java classpath:

  1. script-api.jar (the reference implementation of the scripting API)
  2. jruby-engine.jar (the Java scripting engine for JRuby that I recompiled)
  3. jruby.jar (the JRuby interpreter from your JRuby installation directory)

That's all. Happy scripting/Java mixing!

RSS Feeds
Blogroll
 
Search