# Ruby and java

**URL:** <https://rubytalk.org/t/ruby-and-java/60528>\
**Category:** ruby-talk\
**Created:** [20 October 2010 06:32 UTC](https://rubytalk.org/t/ruby-and-java/60528 "2010-10-20T06:32:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruben\_Herman](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@Ruben\_Herman](https://rubytalk.org/u/Ruben_Herman)\
**Post date:** [20 October 2010 06:32 UTC](https://rubytalk.org/t/ruby-and-java/60528/1 "2010-10-20T06:32:01Z")

</div>

I have a .jar file and want to use some methods form this jar file in  
ruby without using jruby.  
can someone help me?  
thank you!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [20 October 2010 06:51 UTC](https://rubytalk.org/t/ruby-and-java/60528/2 "2010-10-20T06:51:18Z")

</div>

Why don't you want to use JRuby? It's really straightforward, you can  
basically interact with the Java as if it were Ruby (except sometimes you  
have to help it figure out the typing).

> **···**
>
> On Wed, Oct 20, 2010 at 1:32 AM, Ruben Herman \<nedumeritulcj@yahoo.com\>wrote:
> 
> > I have a .jar file and want to use some methods form this jar file in  
> > ruby without using jruby.  
> > can someone help me?  
> > thank you!
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![arton](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@arton](https://rubytalk.org/u/arton)\
**Post date:** [20 October 2010 10:51 UTC](https://rubytalk.org/t/ruby-and-java/60528/3 "2010-10-20T10:51:09Z")

</div>

Hi,

I believe RJB (Ruby Java Bridge) is for you.  
[http://rubyforge.org/projects/rjb/](http://rubyforge.org/projects/rjb/)  
Rjb is Ruby extension library for 1.8.x or 1.9.1 or later and Rubinius.  
And it was tested on OS X, some Linux, Windows with JSE 1.6.  
But it has restriction for multithreading and may not run with Swing,  
AWT etc. So if you need GUI, jruby is your choice.

You can find some document on [http://www.artonx.org/collabo/backyard/?RubyJavaBridge](http://www.artonx.org/collabo/backyard/?RubyJavaBridge)

- easy start:  
set JAVA\_HOME environment variable to your JDK installed directory.  
gem install rjb

script sample)

require 'rjb'  
require 'rjbextension' # for direct jar load

require 'full-path-name-of-jarfile.jar'

Rjb::load # load JVM in Ruby's  
FooBar = Rjb::import('Foo.Bar.YourClass') # load Class into FooBar  
foobar = FooBar.new # instantiate from the class  
foobar.call\_instance\_method(and arguments) # invoke it.

> **···**
>
> --  
> arton \<[artonx@yahoo.co.jp](mailto:artonx@yahoo.co.jp)\>
> 
> --------------------------------------  
> Get the new Internet Explorer 8 optimized for Yahoo! JAPAN  
> [http://pr.mail.yahoo.co.jp/ie8/](http://pr.mail.yahoo.co.jp/ie8/)

---

<div class="post-metadata">

**Author:** ![namekuseijin](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@namekuseijin](https://rubytalk.org/u/namekuseijin)\
**Post date:** [20 October 2010 18:00 UTC](https://rubytalk.org/t/ruby-and-java/60528/4 "2010-10-20T18:00:34Z")

</div>

Mu.

> **···**
>
> On 20 out, 04:32, Ruben Herman \<nedumeritu...@yahoo.com\> wrote:
> 
> > I have a .jar file and want to use some methods form this jar file in  
> > ruby without using jruby.  
> > can someone help me?

---

<div class="post-metadata">

**Author:** ![Ruben\_Herman](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@Ruben\_Herman](https://rubytalk.org/u/Ruben_Herman)\
**Post date:** [20 October 2010 06:57 UTC](https://rubytalk.org/t/ruby-and-java/60528/5 "2010-10-20T06:57:26Z")

</div>

Josh Cheek wrote in post #955656:

> **···**
>
> > On Wed, Oct 20, 2010 at 1:32 AM, Ruben Herman \> \<nedumeritulcj@yahoo.com\>wrote:
> > 
> > > I have a .jar file and want to use some methods form this jar file in  
> > > ruby without using jruby.  
> > > can someone help me?  
> > > thank you!
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > Why don't you want to use JRuby? It's really straightforward, you can  
> > basically interact with the Java as if it were Ruby (except sometimes  
> > you  
> > have to help it figure out the typing).
> 
> I don't want to use Jruby because my ruby code will be use by a tool  
> that is based on ruby not jruby (sorry for my english).
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [20 October 2010 11:21 UTC](https://rubytalk.org/t/ruby-and-java/60528/6 "2010-10-20T11:21:59Z")

</div>

Ruben Herman wrote in post #955658:

> Josh Cheek wrote in post #955656:
> 
> > > I have a .jar file and want to use some methods form this jar file in  
> > > ruby without using jruby.  
> > > can someone help me?  
> > > thank you!
> > > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > Why don't you want to use JRuby? It's really straightforward, you can  
> > basically interact with the Java as if it were Ruby (except sometimes  
> > you  
> > have to help it figure out the typing).
> 
> I don't want to use Jruby because my ruby code will be use by a tool  
> that is based on ruby not jruby (sorry for my english).

Then you'll have to start up an external jvm process (e.g. using  
IO.popen), and talk to some Java code over stdin/stdout or over a  
socket. You can't just "use some methods" directly.

It may make more sense to run a separate jruby instance, because then  
you could talk from ruby to jruby using DRb. This should be easy, given  
that the target machine clearly already has a JVM.

You may be able to migrate your existing ruby code to jruby; the main  
reason I can see for not doing this is if your ruby code uses C  
extensions.

> **···**
>
> > > On Wed, Oct 20, 2010 at 1:32 AM, Ruben Herman \>\> \<nedumeritulcj@yahoo.com\>wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
