# Rjb: Java classes in Rails

**URL:** <https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298>\
**Category:** Rails\
**Created:** [September 28, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298 "2009-09-28T19:32:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 28, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298/1 "2009-09-28T19:32:16Z")

</div>

Hi,

For those who might not be aware, rjb is a killer gem for using java  
classes inside your rails app (or any ruby code). Check it out at:  
[http://rubyforge.org/projects/rjb/](http://rubyforge.org/projects/rjb/)

I was able to successfully use it in one of my apps that required some  
code that is only available in java. And it’s much nicer than having to  
compile a java app, then shell out using backticks, get back the return  
value from the java app, split the strings, etc.

Last update to rjb was in this month, so the gem seems to be well  
maintained by its author 🙂

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 28, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298/2 "2009-09-28T20:52:08Z")

</div>

Fernando P. wrote:

> Last update to rjb was in this month, so the gem seems to be well  
> maintained by its author 🙂

+1

Been using this for a couple months for access to iText. Works great.

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 29, 2009, 9:46am UTC](https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298/3 "2009-09-29T09:46:06Z")

</div>

> +1
> 
> Been using this for a couple months for access to iText. Works great.  
> Oh by the way, as a best practice, where would you put the following:

require ‘rjb’  
Rjb::load()  
JavaClass = Rjb::import(…)

Currently only one of my model uses rjb, so I put at the top of the file  
outside the class declaration, but what if another model also needs rjb  
access, and I put another Rjb::load, would it load the jvm twice? Should  
I move it to a more common place like in an initializer?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 29, 2009, 9:53am UTC](https://www.ruby-forum.com/t/rjb-java-classes-in-rails/176298/4 "2009-09-29T09:53:59Z")

</div>

While I’m at it, if you use textmate, can you run unit tests that use  
rjb?

When I run the tests from console they pass, but from textmate,  
interpreter tells me that the java method calls I make don’t exist, so I  
guess rjb or jvm is not properly loaded.
