JRuby Quick Start

This quick start is using the experimental "complete" jar, which allows the JRuby distribution to be contained in a single jar and extracted easily.

  • Download jruby complete jar
  • In a command shell, go to a directory where you wish to install JRuby.
  • Extract a JRuby install from it:
    java -jar jruby-complete-0.9.8.jar --command extract jruby
    
  • Now we have a JRuby install. Go ahead and download an additional jar that we'll need later for running Rubygems and Rails with ActiveRecord-JDBC; put this in the "lib" directory of your new JRuby install.
  • To use the install, we can simply add /parent/dir/jruby/bin to our path and continue.
    # UNIX
    PATH=/parent/dir/jruby/bin:$PATH
    rem Windows
    set PATH=c:\parent\dir\jruby\bin;%PATH%
    
  • We now have "jruby", "jirb", and "gem" commands available to us. You can also access any bin-directory-installed script with the "--command" argument to JRuby.
    jirb
    irb(main):001:0> quit
    jruby --command irb
    irb(main):001:0> quit
    
  • JIRB stands for JRuby IRB. IRB as you probably know is Interactive RuBy. We can do regular Ruby things in irb, but using JRuby's java integration, we can also create and use Java objects. Enter the following code, one line at a time, in IRB.
    require 'java'
    
    frame = javax.swing.JFrame.new "Hello JIRB"
    frame.set_default_close_operation javax.swing.JFrame::EXIT_ON_CLOSE
    frame.set_size 200, 200
    
    button = javax.swing.JButton.new "Press me"
    frame.content_pane.add button
    
    frame.visible = true
    # frame shows with the button
    
    class Clicked < java.awt.event.ActionListener
      def actionPerformed(e)
        @count ||= 0
        @count += 1
        e.source.text = "You pressed me #@count times."
      end
    end
    
    button.add_action_listener Clicked.new
    # button is clickable and shows count
    
  • You install Rails just like with regular Ruby, with Rubygems. You may wish to disable RDoc generation to speed up the install process as it is quite a bit slower in JRuby.
    jruby --command gem install rails --include-dependencies --no-ri --no-rdoc
    
  • Once Rails is installed, we can generate a play application:
    jruby --command rails playground
    ...
    cd playground
    
  • And we can start Ruby's embedded web server WEBrick:
    jruby script/server
    => Booting WEBrick...
    => Rails application started on http://0.0.0.0:3000
    => Ctrl-C to shutdown server; call with --help for options
    [2007-02-23 14:20:21] INFO  WEBrick 1.3.1
    [2007-02-23 14:20:21] INFO  ruby 1.8.5 (0) [java]
    [2007-02-23 14:20:22] INFO  WEBrick::HTTPServer#start: pid=15244180 port=3000
    
  • And visit http://localhost:3000 in a browser. JRuby on Rails is up and running!
  • We can use ActiveRecord-JDBC to create a database-backed JRuby on Rails application. A little setup is needed.
    jruby --command gem install ActiveRecord-JDBC --no-ri --no-rdoc
    
  • We also need a bit of custom code in environment.rb to load in the JDBC adapter. See the ActiveRecord-JDBC documentation and Running Rails with ActiveRecord JDBC for details.
    # config/environment.rb: Put this before the Rails initializer
    require 'rubygems'
    gem 'ActiveRecord-JDBC'
    require 'jdbc_adapter'
    
  • Add this bit of config to your database.yml to use HSQLDB:
    # config/database.yml
    development:
      adapter: jdbc
      username: sa
      password:
      driver: org.hsqldb.jdbcDriver
      url: jdbc:hsqldb:db/dev.db
    
  • Now let's create a model:
    jruby script/generate model post
    
  • Edit the migration, add the table structure, and run the migration.
    # db/migrations/001_create_posts.rb
      def self.up
        create_table "posts", :force => true do |t|
          t.column :title, :string, :limit => 100
          t.column :updated_on, :datetime
          t.column :content, :text
        end
      end
    
      def self.down
        drop_table "posts"
      end
    
    jruby --command rake db:migrate
    
  • In progress, more to come. I'm still troubleshooting portions of the script -- there currently appear to be some issues with HSQL and migrations.