JRuby (the 100% pure Java implementation of the Ruby programming language) has been gaining more and more attention in the Java and Ruby communities. In my first JRuby article, "JRuby: Java and Ruby Together at Last," I showed some basic usage of JRuby, and closed the article with a small Swing sample written in JRuby. At that time, the current version of JRuby was 0.9.1. Since then, version 0.9.2 has been released with improved support for Rails. With this current release, the ability to run a simple Rails application has been added. In this article, you will upgrade to JRuby 0.9.2, create a simple Rails application, and run it on JRuby.
The Current State of Ruby on Rails Support in JRuby
With three official releases under its belt (0.9.0, 0.9.1 and 0.9.2), JRuby is almost at the milestone of fully supporting Ruby on Rails. This means that Ruby on Rails applications will be able to run on JRuby without modification. JRuby developers have been working hard to get JRuby to pass all tests of the Ruby on Rails test suite and as Charles Nutter (lead JRuby developer) notes in his latest Rails support status update, they are very close. With that in mind, you can get a Rails application up and running on JRuby to see what it entails. First, you need to upgrade to the latest JRuby version.
Upgrading to JRuby 0.9.2
The JRuby distribution comes as a tar.gz file. For this article, I will be working on a Windows-based system running Java SE version 1.5.0_10. The latest JRuby version is available at http://dist.codehaus.org/jruby/. The file to download is jruby-java5-bin-0.9.2.tar.gz. Uncompress the archive; you should end up with a jruby-0.9.2 folder which contained subfolders: bin, docs, lib and samples. You can add a JRUBY_HOME environment variable pointing to this directory and then add the %JRUBY_HOME%bin ($JRUBY_HOMEbin on UNIX) to your system PATH. The JRuby distribution's bin directory contains the jruby.bat file that is used to run the JRuby interpreter. Run the command jruby .version from the command line to test that the JRuby is working:
Installing Rails into JRuby
Now that you have JRuby 0.9.2 installed, you can install Rails into your JRuby installation. You will use JRuby's 'gem' command to download and install Rails:
The '.y .-no-ri .-no-rdoc' command line switches tell gem to install all dependencies, but skip the installation of Rails documentation (because this takes a very long time). As can be seen from the console, you installed rails, rake, activesupport, activerecord, actionpack, actionmailer, and actionwebservice.
So, you now have Rails installed in JRuby. You can verify this by looking into the JRuby installation directory:
As you can see, the version of Rails that was installed into JRuby is version 1.2.2.
Creating Your First JRuby on Rails Application
You now can create your first JRuby on Rails application. Because you are running JRuby on Rails on Windows, you need to perform one extra step to allow you to use the Windows 'rails' and 'rake' commands. I assume you have an installation on Ruby and Ruby on Rails on your hard drive. You will need to copy the 'rails.cmd' and 'rake.cmd' files from the 'bin' directory of your Ruby installation over to the 'bin' directory of your JRuby 0.9.2 installation:
You will need to Issue to command '%JRUBY_HOME%/bin/rails myfirstjrorapp' to create a JRuby on Rails application called 'myfirstjrorapp'.