Installing JRuby and JRuby on Rails with MySQL

July 24th, 2008  | Tags:

Last week I watched a great lecture about JRuby and JRuby on Rails by Leonardo Marcelino. Although I have been studying Ruby and Ruby on Rails for a while, I’ve never had contact with JRuby. Honestly, I’m impressed how JRuby looks good.

Installing JRuby

First of all, let’s download and install JRuby. Go to http://dist.codehaus.org/jruby/ and get the latest JRuby version. When I’ve written this article, the latest was: JRuby-1.1.2.

Extract JRuby in the directory that you wish and then create a environment variable for it. The environment variable must be the name JRUBY_HOME. In Linux for instance, you can edit the ~/.profile file and add the following:

export JRUBY_HOME=/usr/lib/jruby-1.1.2/

Also, make sure the JAVA_HOME is setup. Put the JRUBY_HOME into PATH variable, thus you can access JRuby wherever you want. My .profile file looks like:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export JRUBY_HOME=/usr/lib/jruby-1.1.2/
PATH="$PATH:$JRUBY_HOME/bin"

After those changes, restart your machine (in LInux) and on console (or command prompt) type: jruby -v. You should see a screen like this:

Installing Rails

Like in Ruby, you can install Rails on JRuby using the gems. Simply type the following:

jruby -S gem install rails

After a while, rails will be installed and ready to be used. But before create our first JRuby on Rails application, let’s install also the JDBC-Adapter. We’re going to use the JDBC (like in Java) to conect into JRuby on Rails application, sounds good, huh?

jruby -S gem install activerecord-jdbc-adapter

Getting the JDBC Driver

If you aren’t familiar with Java, you must to know that Java uses JDBC drivers for each database. In this article, I am using MySQL, however you can use another JDBC (database).

The JDBC Driver for MySQL can be found here. Usually, the JDBC driver is only a .jar file. For mysql, the jar file name is: mysql-connector-java-5.1.6-bin.jar

Creating a Rails application

Let’s get started our first JRuby on Rails application. All the commands are going to be the same, except by the fact to use jruby instead ruby. Then, to create the project, let’s type: jruby -S rails <application name> (in my case, I called the application as jrubytest).

Before change/create any file, copy the JDBC driver to the <application>/lib directory. Thus, when you pack the application to a WAR file, the JDBC driver will be imported as well.

Editing database.yml

Open the file config/database.yml and change the development section, like below:

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

Note: We’re going to use the JDBC adapter, therefore you must setup the driver (each application has your own driver), the URL (if you are not using mysql, check out the database documentation to find out which URL you must use in a JDBC connection), the username and password.

Creating the model

For our test, let’s use the scaffold to create a CRUD of products. Type the following:

jruby script/generate scaffold product name:string

Only for test, one field (description) is enough. Run rake to import the table to database.

jruby -S rake db:migrate

After that, you will be able to run your application. Simply start the webrick: jruby script/server. When the server starts, load the following URL: http://localhost:3000/products.

Play a bit around the application. Try out to add/edit/remove some products and verify if your first JRuby on Rails application is running fine.

This article ends up here. In the next article: Deploying JRuby application on Tomcat, Glassfish and Websphere 6.1, I am going to show up how to package the JRuby application in a WAR file and how to deploy it in a Java Application Server.

I hope this article be useful for anyone. If you have question/comments, fell free to leave a comment.

  1. marlo
    August 1st, 2008 at 09:40
    #1

    This is wonderful:)Thanks for your help…

  2. Elisa Travis
    November 12th, 2008 at 22:15
    #2

    ut1juumynx3cli4r

TOP