# Best way to use Java class in Rails?

**URL:** <https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062>\
**Category:** Rails\
**Created:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062 "2006-12-03T00:06:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/1 "2006-12-03T00:06:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/2 "2006-12-03T15:50:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/3 "2006-12-03T19:35:13Z")

</div>

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”)+ “  
” + “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

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/4 "2006-12-03T19:39:14Z")

</div>

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](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.

> **[Home — JRuby.org](https://www.jruby.org/)**
>
> JRuby is a high performance, stable, fully threaded Java implementation of the Ruby programming language.

- Charlie

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/5 "2006-12-03T23:43:42Z")

</div>

On Nov 23, 2006, at 10:10 pm, Elmar Schraml wrote:

> 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 😃 - 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

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 4, 2006, 11:22am UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/6 "2006-12-04T11:22:53Z")

</div>

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](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/](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.

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 4, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/7 "2006-12-04T14:35:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [December 3, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/best-way-to-use-java-class-in-rails/78062/8 "2006-12-03T20:01:17Z")

</div>

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

peter

2006/12/3, Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
