Ruby for Java programmers, Part I

23 01 2006

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

25 01 2006
Agylen » Ruby for Java Programmers, Part II

[…] Part I is here. […]

8 02 2006
Agylen » Ruby for Java Programmers, Part IV

[…] In the previous installments of this series, I did some experiments trying to make Java and Ruby coexist by using two bridges: rjb and YAJB. This time, I’m going to try to solve the same problem using JRuby. […]

27 02 2006
Agylen » Ruby for Java Programmers, Part V

[…] If you’ve been following the previous articles in this series (Part I, Part II, Part III and Part IV) but are not yet satisfied with the range of solutions I presented for calling Ruby code from Java, here’s another one for you. […]

25 01 2007
2 02 2007

Awesome. Just what I was looking for.

21 01 2008

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: