Running Ruby From Java Using JSR-223

Dec, 15, 2007

Figured out how to run a ruby script from Java today using trunk of the jruby. I am considering using Mule for some stuff at work, but would like to use Ruby code as the components that run in the container. Mule supports scriptable components, so the first step was to ensure I could run somewhat complex Ruby code from Java. I wanted to use JSR-223 so that the code would work for any scripting language, not just Ruby. I had enough problems doing it that I thought I'd share how here.

My first requirement for this whole integration was the ability to use trunk of jruby. The performance improvements they have been making on trunk have made jruby a viable production tool. Unfortunately, the current build of the jruby scripting engine implementation is done against a pre 1.0 jruby version (I think). So the first step was to get the jruby scripting engine working against jruby trunk. I did this by checking out the sources from cvs (see instructions here) and patching them using the following diff:

diff -r1.12 JRubyScriptEngine.java
199c199
<             return runtime.parse(getRubyScript(script), filename, null, 0);