Ruby for Java programmers, Part I

This is the first article in a series that I plan to write in order to describe my experiments with combining together Ruby and Java. The reason I’m doing this is that, as much as I find Ruby on Rails to be fun and agile and all-around great, still I haven’t grasped much of Ruby the language yet, so I sometimes find myself wanting to reuse some Java code (and there’s a helluva lot of it around, free for grabs) instead of thinking about a rewrite.

One of the things I’d really want to be able to do is call Java code from Ruby. There are some projects intended to provide a Ruby-to-Java bridge, even though none of them seems to be really mature. The ones I found mentioned more often seem to be RubyJavaBridge and YAJB.

Let’s start with the former. After downloading and unzipping the file, you should read the provided readme.txt file, as there don’t seem to be instructions on the website for actually performing the build. If you’re under OS X, make sure the JAVA_HOME environment variable is set (should be something like /Library/Java/Home, unless your setup is peculiar). Also, ignore everything about the LD_LIBRARY_PATH variable, again if you’re on OS X.

Then you can try configuring and compiling the sources with:

    ruby setup.rb config
    ruby setup.rb setup

On OS X, this will fail, as the off_t type is used but never declared. I assume it gets defined in one of the included C header files in Linux and Windows, but under OS X it’s to be found in <sys/types.h>, which is not included. You can fix it by adding the following statement:

#include <sys/types.h>

to file ext/rjb.h, around line 25 should be fine. After this, everything should compile and install fine just by following the instructions.

Next I did a series of tests with simple Java types to confirm that the JVM was loaded and that the basic functionality of the bridge was fine. In the coming days I plan to invoke some more complex library and report my findings in further installments of this series.

6 Responses to “Ruby for Java programmers, Part I”


  1. 1 flight16

    Awesome. Just what I was looking for.

  2. 2 MixedContent

    I just tried this today (on OS X 10.4) and got the failure you mention and a couple of others. First, the setlocal() function declared in OS X takes two args, and rjb apparently calls it with zero. This causes the config to decide that HAVE_SETLOCALE should not be set, which causes riconv.c to fail, because while certain macros are conditionally defined #ifdef HAVE_SETLOCALE, a function that uses them is not #ifdef’d out. I hacked around that one, but this one remains, and doesn’t sound good:

    rjb.c:833: warning: pointer targets in passing argument 5 of ‘(*jenv)->SetByteArrayRegion’ differ in signedness

    I notice that this post is almost a year old — is rjb still considered a good tool? The author writes doc as if he’s from Mars.

  1. 1 Agylen » Ruby for Java Programmers, Part II
  2. 2 Agylen » Ruby for Java Programmers, Part IV
  3. 3 Agylen » Ruby for Java Programmers, Part V
  4. 4 Experiences with Ruby handling Java at semanticpool :: thoughts

Leave a Reply