Keeping it Small and Simple


Testing JRuby 1.1 RC2

Filed under: JRuby, Ruby Programming — Tags: , , , — Lorenzo E. Danielsson @ 18:20

Of late my schedule has been a bit more hectic than I had hoped. So I haven’t had as much time I would have liked to play around with JRuby and Rubinius. But the other day I was finally able to find a little time. So I downloaded the latest JRuby 1.1 release candidate, which is RC2.

So far it has worked well. I hear that 1.1 is significantly faster than previous versions of JRuby. I cannot comment on that yet, because I haven’t begun playing around with the types of applications where speed would matter.

Installing it

I downloaded the .tar.gz package and installed it to /opt. Do this (as root):

# cd /opt
# tar zxf /home/lorenzod/dl/jruby-bin-1.1RC2.tar.gz

After that you will want to either (1) add /opt/jruby-1.1RC2/bin to PATH or (2) create a few symlinks. I chose the symlink option.

# JD=/opt/jruby-1.1RC2/bin
# ln -s $JD/jruby /usr/local/bin
# ln -s $JD/jrubyc /usr/local/bin
# ln -s $JD/jirb /usr/local/bin
# ln -s $JD/gem /usr/local/bin/jgem

You can of course add all the links that you need. These were enough for me to get started. Note that I named the link to jruby’s gem jgem.

Testing the compiler

I wanted to see how well the JRuby compiler works. So, I quickly put together the following little test program.

 1 #! /usr/bin/jruby
 3 # Count the number of times the user clicks some buttons.
 5 require java
 7 include_class java.awt.GridLayout
 8 include_class java.awt.event.ActionListener
 9 include_class java.awt.event.WindowListener
10 include_class java.lang.System
11 include_class javax.swing.JButton
12 include_class javax.swing.JFrame
14 class ClickButton < JButton
15   include ActionListener
17   def initialize(text)
18     @count = 0
19     @text = text
20     super "#{@text} (0)"
21     add_action_listener self
22   end
24   def actionPerformed(event)
25     @count += 1
26     self.text = "#{@text} (#{@count})"
27   end
28 end
30 class MainWindow < JFrame
31   include WindowListener
32   include ActionListener
34   def initialize
35     super "Click Counter"
36     set_layout, 1)
37     @total = 0
39     1.upto 5 do |n|
40       button = "Button #{n}"
41       button.add_action_listener self
42       add button
43     end
45     add_window_listener self
46     pack
47   end
49   def actionPerformed(event)
50     @total += 1
51   end
53   # Bah, humbug!
54   def windowActivated(event); end
55   def windowClosed(event); end
56   def windowDeactivated(event); end
57   def windowDeiconified(event); end
58   def windowIconified(event); end
59   def windowOpened(event); end
61   def windowClosing(event)
62     puts "Total clicks: #{@total}"
63     System::exit 0
64   end
65 end

First I tried to run it normally.

% jruby cc.rb

That worked fine. After a few seconds a Swing application popped up on my screen. Next I tried compiling it.

% jrubyc cc.rb
Compiling cc.rb to class ruby/cc
% ls ruby/

To run this program I did the following:

% java -cp /opt/jruby-1.1RC2/lib/jruby.jar:.

It worked. JVM takes a moment or two to fire up, then the application window pops up. Of course, I haven’t looked into the options to jrubyc yet, but at least this was enough to get me started.

Next, I’ll want to try JRuby out in Netbeans. I haven’t tried JRuby on Rails yet, so that is something I want to dedicate some time to. So expect more on JRuby from me in the near future.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: