JRuby: Deploying a Rails Application on Tomcat

Published: 24 Jul 2007

JRuby: Deploying a Rails Application on Tomcat
This is a brief tutorial on how to get a simple Ruby on Rails application up and running on Tomcat.

What you will need

  1. subversion client
  2. Java 5 JDK
  3. Tomcat 5.5.x
  4. JRuby 1.0
  5. Ruby on Rails
  6. ActiveRecord-JDBC gem
  7. goldspike Rails plugin (allows you to create a deployable war from an existing Rails app)
  8. MySQL database
  9. mysql-connector JDBC driver

Assumptions and Notes

It's assumed that you know your way around a Java development environment and have a JDK, Tomcat, MySQL and a subversion client installed already. From there you need to download and install JRuby. Once you have JRuby installed, there are some things to note: 1. Most importantly, I had issues with the fact that I had an existing install of Ruby on Rails installed which caused me problems when installing gems, etc. with JRuby. If you have an existing install of RoR, just be sure you are working with the gem executable in your <jruby-install-dir>/bin and not your existing gem executable associated with RoR. 2. People including myself have had classpath issues when trying to call Java libraries. In general, I've been able to workaround these issues by including library dependencies (in the form of a jar) in the <jruby-install-dir>/lib directory.

Testing your JRuby Install

Once you're confident you have JRuby installed you should be able to run the following command and get a similar result:


Creating Your Ruby on Rails Application in JRuby

Now you're ready to install Ruby on Rails in your JRuby install. Run the following:

gem install rails -y --no-ri --no-rdoc

If you've installed Rails before the output should look familiar. Once it's done you're ready to create a Rails app by running something like:

rails project-name

Now to mix things up a bit. Your Rails application will need to be reconfigured from it's default database configuration to use the ActiveRecord-JDBC gem along with a JDBC driver to connect to a database. First, let's install the ActiveRecord-JDBC gem:

cd project-name gem install ActiveRecord-JDBC

Now we need to modify the database.yml file located in the config directory of your new Rails application:


Here we've basically changed the adapter to JDBC. Now we update the config/environment.rb file by adding the section in the red outline:


Now place the mysql-connector JDBC driver jar in your <jruby-install-dir>/lib and while your at it, drop the jar in your <tomcat-install-dir>/common/lib directory.

Depending how familiar you are with RoR, you may want to add some meat to your application by adding a model or two with something like:

jruby script/generate model Widget

Then edit the resulting migration script (db/migrate/001_create_widgets.rb) to include whatever columns you'd like as a part of the widgets table:


Once you've added the columns in the above script, run the following to actually update the database:

rake db:migrate
rake db:migrate RAILS_ENV=production

You now have the model taken care of. The second command above applies the same changes to the production database. To quickly be able to perform CRUD operations on the model you've just created, run the following:

jruby script/generate scaffold Widget

Install the ActiveRecord-JDBC gem:

sudo gem install ActiveRecord-JDBC

You should get something like the following as a result:


In order to facilitate the process of actually putting together a war from your Rails app that can be easily deployed, the goldspike plugin was created. To install it, run the following from your Rails app root directory:

jruby script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike

Once the goldspike plugin is installed, you can create a deployable war in one easy command:

rake war:standalone:create

You will see it automagically download some Java dependencies which it will get from one of the following locations:

  1. A local artifact: lib/java/jruby-1.0.jar
  2. An artifact in a local maven repo: ~/.m2/repository/org/jruby/jruby/1.0/jruby-1.0/jar
  3. An artifact in a remote maven repo: http://www.ibiblio.com/maven2/org/jruby/jruby/1.0/jruby-1.0/jar

In most cases it will probably hit the ibiblio.com/maven2 repository for at least one dependency. This is useful to know because if you're behind a firewall, there are known issues with fetching remote jars. If you run into this problem, I suggest downloading the dependencies manually and placing them in the local lib/java directory.

After the war is created, you can just drop it in your <tomcat-install-dir>/webapps directory and start Tomcat. You should now be able to access your Ruby on Rails app running on Tomcat from your browser: http://localhost:8080/project-name/widgets/list That's it!

In my next post, I will show you how to call Spring-managed beans from your Rails app.

Apache has come up with some really dedicated servers like TomCat. This particular computer software can be used with a number of other web servers as well. TomCat in particular does not need much augmentation and is pretty much advanced as a security software too. Weblog gurus are well aware of this but rookie bloggers are still in the process of discovering benefits of remote backup on different servers. They are the ones who have become insatiable vouchers of dsl internet. Introduction to any new mashup service offering features like voip service or even wireless internet service amazes them.