Thinking OPEN

JRuby and Glassfish on Rails Part I: Setting Up A Developer Environment (on a Mac)

By Freddy Andersen • Nov 28th, 2008 • Category: Tutorials

In this tutorial, we’ll cover setting up your Mac environment to use Glassfish as the server to run your Ruby on Rails projects. This tutorial should port to a Linux desktop, but we’ve not tested that scenario.

Before You Start

There’s some stuff you’ll need on your system:

  • MacPorts installed and at version 1.6. (http://www.macports.org/install.php )
  • Some diskspace.
  • About 15min.

Meat & Potatoes

Installing JRuby

The latest JRuby port is 1.1.3 so that is what we will use today.

[freddy@wazi]# port list jruby
jruby           @1.1.3          lang/jruby

It’s as easy as entering:

[freddy@wazi]# sudo port install jruby
[freddy@wazi]# ruby -v
jruby 1.1.3 (ruby 1.8.6 patchlevel 114) (2008-11-17 rev 6586) [i386-java]

JRuby uses a separate area to store gems and the Ruby environment, so we will need to install the gems that are needed for our development. This includes the Rails gem.

Installing Rails and Friends

Install Rails and create a test application:

[freddy@wazi]# sudo jruby -S gem install jruby-openssl
[freddy@wazi]# sudo jruby -S gem install rails activerecord-jdbc-adapter
[freddy@wazi]# sudo jruby -S rails wazi_app

Now we need to download the latest MySQL Java (JDBC) driver from https://olex.openlogic.com/packages/mysql-connector. Copy mysql-connector-java-x.x.x-bin.jar to $JRUBY_HOME/lib (/opt/local/share/java/jruby).

Now let’s update the database.yml file to use the JDBC driver:

development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/testapp_development
username: root
password:

We should also make sure the JDBC driver will load. To do this, we edit the environment.rb file with these changes:

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end

Now let’s boot the server in our JRuby environment:

[freddy@wazi]# jruby script/server
=> Booting WEBrick...
=> Rails 2.2.2 application started on http://0.0.0.0:4000
=> Ctrl-C to shutdown server; call with --help for options
[2008-11-17 16:40:34] INFO WEBrick 1.3.1
[2008-11-17 16:40:34] INFO ruby 1.8.6 (2008-11-17) [java]
[2008-11-17 16:40:34] INFO WEBrick::HTTPServer#start: pid=13484 port=4000

Installing the Glassfish gem

Having GlassFish fully installed on your development environment is not always the best thing. If you don’t need it then you don’t want either the hogging of memory, or the administration that comes with a running J2EE container, so I’m going to show you an alternative. Do this:

[freddy@wazi]# jruby -S install glassfish

Whew, wasn’t that easy! :) Now, let’s start the server using Glassfish rather than WEBrick:

[freddy@wazi]# jruby -S glassfish_rails
...
Nov 24, 2008 1:33:55 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3000
...
Nov 24, 2008 1:34:01 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: GlassFish v3 Prelude startup time : Felix(1813ms) startup services(6457ms) total(8270ms)

Nice job! The application is now available at http://localhost:3000/.

Finishing Up

You now have a working environment in which you can develop your Rails application, and test them on your local desktop using a J2EE (Glassfish) container. We also installed JRuby with all the required gems needed and this gives us the flexibility of either using JRuby or Ruby and/or Mongrel/Glassfish for different projects.

As an added bonus, here’s some more information about the Glassfish gem:

-c, --contextroot PATH: change the context root (default: '/')
-p, --port PORT: change server port (default: 3000)
-e, --environment ENV: change rails environment (default: development)
-n --runtimes NUMBER: Number of JRuby runtimes to crete initially
--runtimes-min NUMBER: Minimum JRuby runtimes to crete
--runtimes-max NUMBER: Maximum number of JRuby runtimes to crete
APPLICATION_PATH (optional): Path to the application to be run (default: current)

Related OLEX Packages: glassfish, jruby, rails, ruby
Freddy Andersen

Freddy Andersen
Freddy supports OpenLogic customers from his home in California. Born in Norway, Freddy has been working in open source for almost 10 years. He drives a Ferrari truck when he isn't running (either for exercise or chasing Cade, his two-year-old, or simply because he's had too much Red Bull).
All posts by Freddy Andersen

2 Responses »

  1. My site is also created by Jruby but it seems like that glassfish_gem dies once a while. What could be the problem? If u find the answer plz mail me

  2. Very helpful article!

    There’s a minor error in the Glassfish section:

    jruby -S install glassfish

    should be:

    jruby -S gem install glassfish

Leave a Reply