Getting Started With JRuby

  • Post published:July 22, 2015

JRuby is an implementation of modern Ruby language of Java VM. It brings right parallelism and increased performance. It gets fame due to the multi threading capabilities it introduces to Ruby World. When ever the thread word pops up, first choice always remains Java. This was the most wanted feature that Ruby fans were looking for.

Java VM also have world known and top-notch garbage collector, which has proved its stability in very large scale implementations. It can be deployed to Java Servers and can utilize full power of Java ecosystem. With all that, it still remains Just Ruby Language. Core development team of JRuby always preferred compatibility with Ruby implementation first and then Java VM.

Most recently JRuby launched its 9000 version. This actually has been pronounced from its version ‘jruby-‘. It is the most awaited version and is fully compatible with Ruby 2.2.

Lets start digging it further. You need to use rbenv to install the new version of JRuby. The command to install is:

rbenv install jruby-

You must make sure that you have updated the version of ruby-build plugin. If not, then to update it, you can run this command:

cd ~/.rbenv/plugins/ruby-build && git fetch origin

Lets start running and utilizing its power in a web application also known as Rails Application. After installing JRuby, use rbenv rehash to refresh and then install the rails gem. Rest of the things are identical to any rails application flow.

To test JRuby, you can write any controller and can import Java classes like this:

import java.lang.System
import java.lang.StringBuilder

Whichever Java class you require, you have to manually import it. The difficult part will come up when you don’t find namespace suggestions from your IDE, so you have to rely on Java API documentation to know the exact namespace hierarchy for that class. Rest for creating java class objects is similar with the syntax of ruby:

builder =

Look how easy it is to use Java classes with proper ruby language syntax. If you need to write the same code in Java, it would be similar to:

StringBuilder builder = new StringBuilder(length);

Notice here the difference of new …class and Also toString() is converted to simple to_s. You can utilize all native java classes and objects the same way. You must note that the OOP and language concepts here would be used by Ruby not Java.

Now you reached that point where you need to utilize the pure Java power inside Rails application by using JRuby. Java libraries are normally distributed in jar files. These are compressed files containing byte code of java classes, so whenever you look for any Java library you will get its distribution in jar file. To store those jar files, you have to create a jars directory under vendor. It is the most appropriate location to store jar files. Now you need to load all jars in application. It is similar to require any gem file in ruby, so you need to write a line of code in config/application.rb:

Dir[Rails.root.join('vendor', 'jars', '**', '*.jar')].each { |jar| require jar }

Now each third party Java Library is loaded into the application and you can now import its classes and use it.