The JRuby and Glassfish teams have made installing, building, and launching a JRuby on Rails app a rather trivial process. Glassfish, a Java app server, has been reduced in size from a 22.5 MB install to 2.5 MB Ruby gem (not too shabby for a full app server). If you need the full monty (not covered here) or you need to deploy to another Java app server, install the warbler gem. The combination of Rails 2.0.2 (stable), JRuby 1.1 RC2, the Glassfish gem, and JDK 1.6 is not only breaking down enterprise barriers but speeding up apps all over the place.
So how do we get started with all this, in 10 minutes or less? Let’s take a look.
Overview
Here are the steps we will take to build our first JRuby on Rails application:
- Download and install JRuby
- Install Rails and the dependent gems
- Install the Open-SSL gem
- Install the GlassFish gem
- Create a test JRuby on Rails app
- Deploy our little app to the GlassFish gem
“Make it so Number 1″
First, download JRuby 1.1 RC2. Unpack it into a directory and edit your bash_profile to include the bin directory in your $PATH so you can easily call JRuby executables. Mine looks like this: /Users/rdempsey/Documents/jruby/jruby-1.1RC2/bin. Open up a terminal session and type jruby –version to see if you are successful. If you are, you should see something like this:
$ jruby --version ruby 1.8.6 (2008-02-16 rev 5944) [i386-jruby1.1RC2]
Now that JRuby is installed (and yes, ready to go), let’s install Rails:
$ jruby -S gem install rails --no-ri --no-rdoc Successfully installed activesupport-2.0.2 Successfully installed activerecord-2.0.2 Successfully installed actionpack-2.0.2 Successfully installed actionmailer-2.0.2 Successfully installed activeresource-2.0.2 Successfully installed rails-2.0.2 6 gems installed
For full SSL support (and why not have it) install the jruby-openssl gem too.
$ jruby -S gem install jruby-openssl --no-ri --no-rdoc Successfully installed jruby-openssl-0.1.1 1 gem installed
Now for the Glassfish gem. One command should get us going.
$ jruby -S gem install glassfish Successfully installed glassfish-0.1.1-universal-java 1 gem installed
Great. How are we on time? 8 minutes to go? Perfect. Let’s create our test Rails app.
$ jruby -S rails --database mysql testapp ... Rails code being generated ...
Change into the directory of testapp and fire up your favorite editor. I like TextMate, though the new NetBeans is looking HOT. Once inside, open up config/database.yml and change the username/password for your MySQL server as necessary. In your terminal and in the root of your app, run the following rake command to create the development database:
$ rake db:create
It may not seem like anything happened, but if you fire up your MySQL gui tool of choice (or go command line) you will see that the testapp_development database has been created. Now to create a migration and test things out. Let’s make a widget.
$ script/generate scaffold Widget name:string description:text
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/widgets
exists app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/widgets/index.html.erb
create app/views/widgets/show.html.erb
create app/views/widgets/new.html.erb
create app/views/widgets/edit.html.erb
create app/views/layouts/widgets.html.erb
create public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/widget.rb
create test/unit/widget_test.rb
create test/fixtures/widgets.yml
create db/migrate
create db/migrate/001_create_widgets.rb
create app/controllers/widgets_controller.rb
create test/functional/widgets_controller_test.rb
create app/helpers/widgets_helper.rb
route map.resources :widgets
With Rails 2.0.2 we don’t have to specify the timestamp fields, those are automatically added to the migration. Now, run “rake db:migrate” to add your database table.
$ rake db:migrate (in /Users/rdempsey/Documents/jruby/testapp) == 1 CreateWidgets: migrating ================================================= -- create_table(:widgets) -> 0.4622s == 1 CreateWidgets: migrated (0.4624s) ========================================
And now for the fun part, deploying the app to the GlassFish gem. Change to the parent directory of your application (getting out of the root directory) and run the following command:
$ jruby -S glassfish_rails testapp Feb 18, 2008 5:03:00 PM com.sun.enterprise.v3.server.AppServerStartup run ... GlassFish firing up... INFO: Loading application testapp at / Starting Rails instances ... Rails instantiation info... INFO: Glassfish v3 started in 24766 ms
As with Mongrel, the GlassFish gem starts our server on port 3000. Fire up your browser and go to http://localhost:3000/widgets and play around.

Pretty nice eh? Looks like we have time to spare as well. Go make yourself that next pot of coffee and see what else you can do with this new found power. Let us know how things turn out as well.
Connect with me on LinkedIn, Twitter, or recommend me on Working With Rails.
Other Posts That Might Interest You

