JRuby: Saint Valentine, Bless Java and Ruby

by Denis PetruchikFebruary 14, 2011
This blog post explores JRuby—a Java implementation of the Ruby language—with its advantages and limitations.

In January, the JRuby community announced the 1.6.0.RC1 release of JRuby, a Java implementation of the Ruby programming language. Since we already utilized this tool in one of our Java projects, the release encouraged me to take a deeper look at it and reveal its strong sides and hidden issues.

 

What is JRuby?

JRuby is the interpreter of the Ruby programming language that enables building Ruby scripts and Ruby-on-Rails (RoR) applications into Java implementations. JRuby features some of the similar concepts Ruby has, including object-oriented programming and duck-typing.

With JRuby, you get most of the Ruby/Rails advantages and enable interoperability with Java platform applications, as well as gain access to the full range of Java functionality.

  • You can efficiently use Java classes in Ruby scripts.
  • JRuby can be applied to create solutions for Google App Engine. You can use JRuby as easy as any of the native App Engine languages.
  • JRuby is compatible with all operating systems that allow for running J2SE 1.5 and higher, which means that it can be used to create cross-platform apps, including desktop ones.
  • JRuby enables to run Ruby-on-Rails applications on any Java application server: JBoss, WebLogic, GlassFish, etc.
  • JRuby can be leveraged to create mobile applications—for the Android platform at the moment—via Scripting Layer for Android (SL4A). The library brings scripting languages to Android by allowing developers to edit and execute scripts and interactive interpreters directly on an Android device.
  • JRuby supports RubyGems, a package manager that allows for installing gems locally and remotely (though C extensions are not supported).
  • You can access the existing Spring context from JRuby on RoR applications and use it to work with Spring beans, as well as to enable Hibernate without calling ActiveRecord.
  • Furthermore, JRuby works even faster than Ruby. When I tested Ruby 1.8.7 and JRuby 1.6.0 implementations against each other, the results showed 4–5 times better performance of JRuby due to the dynamic compilation.

 

How to use it?

Java developers can work in an integrated development environment (IDE) of choice, such as Eclipse or IntelliJ IDEA. In particular, Eclipse can be utilized along with the RedRails plugin, while IntelliJ IDEA can be used with Maven, which handles Ruby scripts during the project building. So, IntelliJ IDEA combined with Maven enables to set up a project on any computer, regardless of its operating system and libraries installed.

Another tool Java developers can use is Warbler. Warbler is a gem that transforms the existing Ruby-on-Rails application into a .war file. The intent is to provide a flexible and easy way to pack all application files for deployment to a Java application server.
 

Things to keep in mind

However, there are still some challenges I noticed when using JRuby.

  1. Though you can successfully enable Java classes in Ruby scripts and call Ruby scripts from Java, there are still certain limitations.
    • When using Java APIs in Ruby scripts, it is sometimes necessary to add synchronization on an object for thread safety.
    • Java classes cannot be inherited from Ruby classes.
    • To use a Java class, you have to provide a full path to it (e.g., java.lang.Thread) without using import.
  2. Running JRuby applications takes longer than native Ruby apps, because of the Java Virtual Machine (JVM) launch.
  3. A JRuby application requires more random-access memory (RAM) to run several copies compared to Ruby apps.
  4. The JRuby community is not as large as the Ruby community, although they are both tightly connected.

 

JRuby horizons

With all its strong sides and drawbacks, I think JRuby will occupy its niche in the range of technologies Java and Ruby developers use frequently.

It might be a silver bullet in case you need to integrate disparate systems, given that one of them is written in Ruby, while another one runs on Java. For example, if companies merge with the need to integrate their corporate portals or CRMs for easier maintenance, using JRuby will come in handy. You can reuse the existing Ruby code and add some functionality from a Java library or vice versa.

Using the Java Virtual Machine, you can also build cross-platform apps in Java. As JRuby provides dynamic invocation on JVM, Ruby developers can create applications for multiple operating systems.

I think there is a future for using JRuby on mobile platforms, as well. As mentioned before, JRuby currently enables to create applications for Android. It is also rumored that it will be possible to develop apps for Blackberry in the nearest future.

 

Further reading

 

About the author

Denis Petruchik is Senior Java Developer at Altoros. He is a true expert in Java, Python, and C#, specializing mainly in web development for both Windows and Linux platforms. Denis can boast of strong expertise in design and implementation of relational databases. He has a long track record of creating dynamic applications, applying the best approaches for development, architecture, design, and scaling.

The post was written by Denis Petruchik and edited by Alex Khizhniak.

  •  
  •  
  •