1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Installing and Using JRuby


JRuby can run on all the major platforms, but this tutorial will focus on Windows (it really on differs when it comes to installation and setup). On Linux and OS X, the installation can be rather varied depending on your distro and setup, but Ruby installers such as RVM can usually handle installing it for you.

JRuby can be installed via a number of methods, but the easiest by far (on Windows at least) is to download the installer bundled with the JVM. This is virtually a one click install. It will handle installing JRuby to a simple path like C:\jruby-1.x.x, install the JVM and set up your command line path and classpath.

Once you set up JRuby, test it out by firing up a command line window and enter the commands jruby --version and jirb. The first command should print the version you just installed, make sure they are the same as it's possible to also have an older JRuby installation also in your path. The second starts up the JRuby version of irb. You can enter any Ruby code here and generally play around with it, make sure it all works. When you're finished with it, you can get out of it by typing exit.

Executing Scripts

Great, now we have JRuby installed and we know how to get to IRB (or JRuby's equivalent, at least). But the main development process for Ruby is to, of course, store your Ruby programs in files, so we need to figure out how to execute them. On Linux and OS X, you generally just make the file executable and use the shebang line #!/usr/bin/env ruby. The shell handles the rest. Here on Windows, things are a bit different.

First, you can run any script using the jruby command. This can be done from the command line, such as jruby some_file.rb. Note that the .rb file extension can be a bit important. While on other systems where the shebang line is used, the .rb file extension is really just a reminder to you what type of file it is, on Windows it certainly matters. It's common practice to always use .rb anyway, but it's something you do need to pay attention to.

Next, we can set up a file association. The easiest way to do this is to simply right click on any .rb file and go to properties. Near the top of this dialog, you'll see an "Opens with" field. Click on "change" and navigate to the directory where JRuby is installed and select jruby.bat in the bin directory. You also might want to change the icon, there is a suitable icon in the jruby.exe file in the bin directory. The process is a bit strange, there's no real GUI for it, but exact directions can be found here. I was unable to find a way to have Windows refresh its icon cache using RunDLL, so after changing the icon you need to either log out and in or restart explorer.exe.

However, this will simply run the program and exit immediately. This means any scripts that run and produce output on the command line window will not be usable. The command line window will pop up, the program will run and as soon as it's finished the command line window will close. To fix this, copy jruby.bat to jrubyw.bat, strip out the extra lines and add a pause line to the end.

"jruby.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9

A Text Editor

Windows comes with a text editor called Notepad. It's utterly underequipped to do anything useful, we'll need to replace it. I use two editors on Windows, each have their advantages. And of course there are many, many more. Just pick one you like, the minimum features should be syntax highlighting and indentation, anything more than that is just icing.

  • Notepad++ - This is a go-to text editor of Windows. It has tons of features, it has tons of extensions and doo-dads and everything you can possibly want from an editor. I usually install this as a replacement for Notepad at the very least since it handles line endings properly (it's 2013 Microsoft, you think you'd have fixed that 20 years ago!) and is a nice thing to have around.

  • Sublime Text - This editor is slick. Really slick. It's heavily inspired by TextMate and supports things like bundles popularized by TextMate. It looks really nice, comes with some nice themes and the visual scrollbar is like nothing I've ever seen. I like everything about this editor, I wholly recommend buying it.

Doing Something

Now that we're up and running, let's do something! Let's do something Ruby can't do by itself (typically), let's display a GUI window. Using Swing (the Java GUI toolkit), we'll create a simple window with a label containing a simple message. We'll see how you can easily call Java code as if it's native Ruby code, and we'll make a GUI window in Ruby. Not since Shoes has that been so easy!

require 'java'

import javax.swing.JFrame
import javax.swing.JLabel

class MainWindow < JFrame
    def initialize
   	 super "JRuby GUI Demo"
   	 setDefaultCloseOperation JFrame::EXIT_ON_CLOSE

   	 label = JLabel.new "Hello, JRuby world!"
   	 add label

MainWindow.new.setVisible true

And there you are! A working JRuby environment on Windows and ready to go!

  1. About.com
  2. Technology
  3. Ruby
  4. JRuby
  5. Getting Started with JRuby

©2014 About.com. All rights reserved.