JavaEnterprise JavaRunning Your First Rails Application on JRuby

Running Your First Rails Application on JRuby

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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’.

Change to the ‘myfirstjrorapp’ directory and run the command ‘jruby scriptserver’. This will start the Rails built-in ‘WEBrick’ web server and start your application on port 3000:

If you get an error such as the following:

It means that your Ruby on Rails installation is older than the JRuby on Rails installation and therefore the ‘myfirstjrorapp’ is looking for the older Ruby on Rails version. In your case, the Ruby on Rails version is 1.2.1, but the JRuby on Rails version is 1.2.2. To fix this, edit the ‘myfirstjrorappconfigenvironment.rb’ file and update the RAILS_GEM_VERSION variable:

# Specifies gem version of Rails to use when vendor/rails is not present

RAILS_GEM_VERSION = '1.2.2' unless defined? RAILS_GEM_VERSION

Now, you can point your browser to http://localhost:3000/. you should see the following:

That is it. You have created your first JRuby on Rails application!

Conclusion

This article has provided you with a step-by-step process to install Rails into your JRuby installation and get a simple Rails application up and running. Take some time to look further into JRuby and Ruby on Rails and see what it has to offer you.

References

About the Author

Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java, Ruby, and .NET-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories