javarubyjrubyrjb

Using Java from Ruby: JRuby vs. Rjb


Can you comment on the strengths and weaknesses of each in terms of:


Solution

  • I haven't used rjb, but it appears to be similar, in concept, to JPype, a Python-Java bridge that I have used. Both appear to load the JVM runtime, as a shared object or DLL, into the running Python or Ruby interpreter.

    In my experience, this approach works fine, until it doesn't, and when it doesn't work, it tends to fail catastrophically. The problems I ran into with JPype were related to differing assumptions that the Java runtime and the Python runtime made about the operating environment. Areas of concern include:

    I've had enough bad experiences with that approach that I'm wary of it.

    Having said that, however, rjb is not JPype, and Ruby is not Python. The Ruby threading model may coexist better with the JVM than the Python threading model did. Also, I haven't played with technologies like this for more than two years, so things may have changed.

    Bottom line: It may work fine, but be careful.

    As to your specific issues:

    Performance

    I'm not even going to hazard a guess here, because the performance characteristics of either approach are going to depend a lot on what you're doing, and you haven't outlined your intended use of either technology.

    Cross Platform

    Anything pure Java (e.g., JRuby) will port cleanly anywhere there's a Java VM. This is not necessarily true of an rjb solution. You may run into incompatible shared library issues, for instance. You may find you have to build rjb on a platform, necessitating building a lot of other stuff first. Etc.

    On the other hand, the problem you'll run into with JRuby is that a lot of gems just aren't available. In the Java world, JNI (i.e., bridging out to C or C++ code) is generally frowned upon. The "best" code is 100% Java. In the Ruby world (and, for that matter, the Python world), bridging out to C APIs is common practice. Loads of gems do that (e.g., database drivers, some gems that leverage existing open source C APIs, some gems that really want kick-ass performance). The bridge between C-Ruby and C is totally different than the bridge between Java and C. Gems written for C-Ruby that link in C code simply won't work, as is, in JRuby. So, porting code from C-Ruby to JRuby can be problematic.

    Coverage of Java's Features

    Since rjb loads the JVM into the Ruby interpreter, it should be able to support anything the JVM supports--though the interface between Ruby and Java can get clunky, at least according to the documentation. Since JRuby is actually implemented entirely in Java, the interface between JRuby and Java tends to be a little cleaner.