This post originated from an RSS feed registered with Agile Buzz
by Jared Richardson.
Original Post: You Got Your Java In My Ruby Code!
Feed Title: Jared's Weblog
Feed URL: http://www.jaredrichardson.net/blog/index.rss
Feed Description: Jared's weblog.
The web site was created after the launch of the book "Ship It!" and discusses issues from Continuous Integration to web hosting providers.
I'm working on a project that requires both Ruby and Java and I was surprised at several rather mature projects that make this easy.
First I looked at the best known project in this space, . JRuby is an amazing project. They are running Ruby code from with Java code. And it works very nicely. If you're working on a Java project but you could make more time if you had access to some Ruby bits, check this one out.
However, for my project, JRuby wasn't appropriate. I'm trying to write another Rails database adapter (this time for the Hypersonic database) and the database is written in Java. Using JRuby would've meant keeping up a Java faux-server to listen for connections from my Ruby client. That didn't seem ideal to me.
I found two other projects that looke promising though. First, the Ruby Java Bridge (RJB). Unfortunately, the "out of the box" experience wasn't so good for me... it didn't work on my Mac laptop.
When I was hitting the search engines to see if anyone else had run into my issues, I found Yet Another Java Bridge. YAJB just worked. :) You can't beat a one line install... ruby setup.rb.
Using the bridge is trivial. Here's the obligatory Hello World code:
require 'yajb/jbridge'
include JavaBridge
jimport "javax.swing.*"
:JOptionPane.jclass.showMessageDialog( nil, "Hello World!")
That's Ruby code that pops up a Java dialog... not bad! I'm not going to code it up to prove it, but this looks like less code than you could use to do the same in a Java only program.
I can see projects like YAJB becoming a very popular way to wrap your legacy Java code so that projects like Ruby on Rails can access it.