JRuby on Rails with Nine Lives: Running a JRuby on Rails Application on Tomcat
If you have not heard the buzz about JRuby and Rails, you must be living under a rock. The Java, Ruby and Rails communities are all excited with possibilies that JRuby and Rails gives to web application developers. Java has been used for more than 10 years in desktop, mobile, web, and enterprise application development. Ruby and Rails are the twin towers of web application agility and coolness and are igniting the web and software application development community with their pragmatic practices. With JRuby, Ruby, and Rails, applications can now run on top of JVM and use the vast Java frameworks and libraries available.
In my previous article, "Running your First Rails Application on JRuby," I showed you how to get JRuby installed and create and run a basic non-CRUD Rails application within it. This was simple and painless and demonstrated JRuby's initial support Rails. Since then, JRuby has been enhanced to fully support Rails and is now at version 1.0 status. In this article, you use JRuby to create a simple CRUD-based Rails application for managing a list of guitars (similar to the one in my "Skip the CRUD" article) and run it on the Java-based Tomcat application server. This will demonstrate JRuby's improved support for Rails applications by using database access via JDBC and the ability to run a Rails application on a Java application server.
For this article, I will use a machine running the Ubuntu Linux distribution. I am going to assume you already installed JRuby 1.0 into the /opt/jruby-1.0 directory. I create a symbolic link in /opt/jruby pointing to the JRuby install directory. You will also need to create a $JRUBY_HOME environment variable pointing to the /opt/jruby directory so you can run JRuby commands anywhere. The following screenshot shows JRuby 1.0 installed in /opt:
Figure 1: JRuby installation
Installing Rails in JRuby and Creating Your Rails Application
Installing Rails into JRuby is as simple as running the following command from your $JRUBY_HOME directory:
jruby -S gem install rails -y -no-ri -no-rdoc
Figure 2: Installing Rails into JRuby
The S switch tells JRuby to run the command in JRuby's bin directory.
You'll need to install the ActiveRecord-JDBC gem as well. This will allow Rails' ActiveRecord to use Java's native JDBC driver to access the database:
jruby -S gem install activerecord-jdbc
Figure 3: Installing ActiveRecord-JDBC into JRuby
Now, create your JRuby on Rails application. I change directories to my local JRuby apps directory /home/dsuspense/jruby/apps and create a railswartest application:
jruby S rails railswartest