Best way to use Java class in Rails?

Hi,

I'm currently trying to convince my boss to let me do a new web app in
Ruby on Rails.

However, it needs to use at least one fairly big and complex class that
is currently written in Java.

I could:

- run rails on JRuby:
  no problem to call the Java class, but how well does Rails run on
JRuby? The JRuby site says something like "it probably works, but we're
not sure". Has anybody tried Rails on JRuby for a real application?

- write a command-line interface for the java class, and call it as a
shell command from ruby.

- wrap the java class in a servlet and call it via http (I only need it
to do some complex processing on some strings)

- re-implement the class in Ruby (a lot of work, and twice the
maintenance in the future)

Any thoughts on what would be the best option?
I'd be particularly interested in hearing from anybody who tried or is
running rails on JRuby.

Regards,
Elmar

2 or 3 would be nice. 2, if the traffic isn’t that heavy. I tried out Jruby, simple Rails app seem to work. A lot of plugins (backgroundrb, e.g.) fail.

Vish

Hi Elmar,

Well, JRuby is certainly one way. So too is YAJB. I'll show you how to
do it both ways below. Suppose I have a Java class called des with a
method called encrypt:

require 'yajb/jbridge'
include JavaBridge
class ApplicationController < ActionController::Base

  def index
       render_text desencrypt("Hello There
World!","somelengthypassword")+ "<br>" + "Hello There World!"
  end

  def desencrypt(s,password)
    #render_text JBRIDGE_OPTIONS[:classpath]
      jimport "javax.swing.*"
      des = jnew :DES, password
      s = des.encrypt(s)
      #use the folowing if using jruby
      # #DES must be on the System's CLASSPATH
      # require 'java'
      # include_class 'DES'
      # des = DES.new(password)
      # s = des.encrypt(s)
    end

    def desenc()
      render_text desencrypt(params[:newitem],"somelengthypassword")
     end

# - Ralph

Elmar Schraml wrote:

- run rails on JRuby:
  no problem to call the Java class, but how well does Rails run on
JRuby? The JRuby site says something like "it probably works, but we're
not sure". Has anybody tried Rails on JRuby for a real application?

I think it's a little more positive than that these days. We've been
making a serious effort to get Rails working better, and it's paying
huge dividends so far. ActionPack, ActiveSupport, and ActionMailer are
almost 100% now, and ActiveRecord is probably around 75%. Most apps
work pretty well now, and there are folks starting to run JRuby on
Rails apps for small production applications.

Testing results are shown here:

http://www.headius.com/jrubywiki/index.php/Rails_Support

As someone else mentioned, there are certainly things that don't work.
Specifically, things that depends on marshalling heavily (DRb-based
stuff) or obviously anything that uses a C extension. But it's not
quite so grim as "it may work, we don't know"...we're just being
cautious about saying it's "officially supported" until we're closer to
100% on all of Rails' own tests.

Feel free to join the JRuby lists if you have questions or need some
help getting going.

www.jruby.org

- Charlie

are there any efforts the oppsite direction to integrate java in ruby (not ruby in java, like in jruby) ???

peter

However, it needs to use at least one fairly big and complex class that
is currently written in Java.

...

- wrap the java class in a servlet and call it via http (I only need it
to do some complex processing on some strings)

- re-implement the class in Ruby (a lot of work, and twice the
maintenance in the future)

Elmar,

If the big and complex class is typical Java (and if it's doing text processing, it surely is), this is a possibility:

* Re-implement the class in Ruby, in 25% or so of the code
* Port your unit tests - to rspec :smiley: - so you can show it functions identically
* Expose it as a web service (really trivial with REST support) and have the Java app call it

This way you just *reduced* your maintenance

Of course, maybe something about the code I don't know about would prohibit it, but it's a thought

Ashley

Thanks to everybody who replied.

And since we're collecting solutions to this problem here, I'd like to
mention that I ended up using yajb (yet another java bridge -
http://www.cmt.phys.kyushu-u.ac.jp/~M.Sakurai/cgi-bin/fw/wiki.cgi?page=YAJB
), and there's another java bridge called rjb
(http://rjb.rubyforge.org/ ) that seems to be more actively developed.

Documentation for both is kind of spotty, but for my kind of purpose
(just needing to occasionally use a small number of java classes in
ruby) it's perfect.

Nice to hear that JRuby is coming along, too, that will probably be the
preferred way in the future.

Hi Elmar,

Yes, I looked at RJB, but it seemed a little more arcane to use than
YAJB (even though the latter's doc's are transcriped from Japanese).
However, JRuby certainly has the support and enthusiasm out there for
it, so that may be the ultimate winner and I would not integrate Java
classes into my app without considering, at each "touch point" in the
code, how that would be done. You may want to do it both ways actually.
-Ralph