JRuby Bridge Tutorial - Integrating Java and JRuby


This short tutorial shows one of several possibilities to use a Ruby application within a Java application. It contains one complete example using Windows 7 (doing the same on OSX or Linux should not be difficult).

JRuby can be downloaded here. Use JRruby 1.7.23 (this corresponds to Ruby 1.9.3) for applications which are supposed to run on Java 1.6 (a.k.a. Java 6) or higher, and the most recent JRuby 9.x (this corresponds to the Ruby 2.2 line) for applications running on Java 1.7 (a.k.a. Java 7) and higher. You will need two download distributions: One is the platform-specific distribution, which you need for executing a jruby command - this would be for example JRuby 1.7.23 Windows Executable (x64). The other one is the (platform-independent) jar file, which is used to deliver the application, i.e. running it with java. This is called jruby-complete, and on the download page it would be found under the name of, for instance, JRuby 1.7.23 Complete .jar.

You can verify your JRuby- and Java-version after installation with

jruby --version

This also shows the equivalent Ruby version implemented by JRuby, for example:

jruby 1.7.23 (1.9.3p551) 2015-11-24 f496dd5 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_79-b15 +jit [Windows 7-amd64]

Reason for writing this tutorial

While documentation and blogs exist, which describe various aspects of the interaction of JRuby and Java, I had some difficulties to find all the pieces and put them together. I wished I had found a concise tutorial. Since I couldn't fine one, I wrote it myself.

I would like to express my gratitude to the users at the JRuby Forum, the JRuby Mailing list, and StackOverflow, for their help.

The Example

The example application, which also can be downloaded, comprises of a Java main program, and classes written in Java and Ruby. One of those Java classes "talks" to the Ruby world, passes Java objects to Ruby and returns Ruby objects as well as Java objects created within the Ruby world. The application doesn't do anything useful and just demonstrates various aspects of the interoperability between Java and JRuby.

I don't show every possibility to connect Java and Ruby, but enough to allow anyone build a mixed Java-Ruby-application.

The connection between Java and JRuby is known under the name The Red Bridge.

I tested the example with Java 1.7 and JRuby 1.7.23 on Windows 7.