Forum: JRuby [HELP] - How to call java method from jruby?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Muthu Selvan SR (Guest)
on 2013-06-22 08:24
(Received via mailing list)
Hi All ,

I am new jruby and i want call java coding from jruby ...but i am
the following error ...

*CODE :*
$ cat java-ruby.rb
require 'java'
m =
m.put "hello", "world"
m.put "goodbye", "life"
puts m.get("hello")

iter = m.keySet.iterator
  while iter.hasNext
  key =
puts "#{key}=#{m.get(key)}"


$ ruby java-ruby.rb
`require': cannot load such file -- j*ava (LoadError)*
from java-ruby.rb:1:in `<main>'
mselv2m1:ruby_Code mselv2$

Thanks ,
Muthu Selvan SR
Mark Mandel (Guest)
on 2013-06-22 10:26
(Received via mailing list)
From the stack trace, looks like you are trying to do this from Ruby,


On Sat, Jun 22, 2013 at 4:21 PM, Muthu Selvan SR

> require 'java'
> Thanks ,
> Muthu Selvan SR


2 Devs from Down Under Podcast
Muthu Selvan SR (Guest)
on 2013-06-22 20:34
(Received via mailing list)
Hi ,

I have used "required 'java'" in my ruby code , please let me know
what should i do to run this java code from JRuby ?

Thanks ,
Muthu Selvan SR
0d1cd73bfe0ebe09074bfcd138ed3d5d?d=identicon&s=25 Keith B. (keith_b)
on 2013-06-22 20:53
(Received via mailing list)
Muthu -

The "2.0.0-p195" in the error output shows that you are running this
script with MRI Ruby 2.0, not JRuby.  JRuby is a different Ruby
implementation.  If you haven't installed it, you can do easily with rvm
(onUnix based systems), or by Googling "installing jruby" and following
the links.  If you have already installedit, then you need to find out
why it's not being called; it could be as simple asthat you need to call
"jruby" instead of "ruby" to run the script.

- Keith

Keith R. Bennett
Muthu Selvan SR (Guest)
on 2013-06-23 20:17
(Received via mailing list)
Hi  ,

Already i have installed jruby successfully , please refer my below
output for successful installation in jruby

*$ jruby -S gem list*

*** LOCAL GEMS ***

awesome_print (1.1.0)
blankslate (
builder (3.2.2)
bundler (1.3.5)
bundler-unload (1.0.1)
calabash-common (0.0.1)
calabash-cucumber (0.9.150, 0.9.148)
CFPropertyList (2.2.0)
cucumber (1.3.2)
diff-lcs (1.2.4)
edn (1.0.2)
geocoder (1.1.8)
gherkin (2.12.0)
httpclient (2.3.3)
json (1.8.0)
location-one (0.0.10)
mini_portile (0.5.0)
multi_json (1.7.7)
parslet (1.4.0)
rack (1.5.2)
rack-protection (1.5.0)
rake (10.0.4)
ruby-ole (
rubygems-bundler (1.2.0)
rubygems-update (2.0.3)
rubyXL (1.2.10)
run_loop (0.0.15, 0.0.12)
rvm (
sim_launcher (0.4.6)
sinatra (1.4.3)
slowhandcuke (0.0.3)
spreadsheet (0.8.5)
thor (0.18.1)
tilt (1.4.1)

$ *jruby -e "puts [1,2,3].minmax"*

Please let me know why i am not able to run java code jruby ?

Thanks ,
Muthu Selvan SR
773c21073bd45af7c4f90ce516827752?d=identicon&s=25 Eric W. (edubkendo)
on 2013-06-23 20:33
(Received via mailing list)
@Muthu, just like you ran $ *jruby -e "puts [1,2,3].minmax" , *  in
to run your script you need to run $ *jruby* java-ruby.rb , instead of $
ruby java-ruby.rb  , which, from the looks of your stacktrace, and as
others have already pointed out, is calling on the MRI ruby that appears
be already installed on your system and on your path. There are many
to get around this, from using a version manager such as RVM or rbenv,
managing your path yourself, to aliasing ruby to the jruby executable
(which I am NOT reccomending), but to simply run your script, you need
actually run the jruby executable instead of the MRI ruby one which
to already be on your path .
Hassan Schroeder (Guest)
on 2013-06-23 20:39
(Received via mailing list)
On Sun, Jun 23, 2013 at 11:14 AM, Muthu Selvan SR
<> wrote:

> $ jruby -e "puts [1,2,3].minmax"
> 1
> 3

>>>> $ ruby java-ruby.rb
>>>> `require': cannot load such file -- java (LoadError)

> Please let me know why i am not able to run java code jruby ?

Because you can't see the difference between 'ruby' and 'jruby'?

Hassan Schroeder ------------------------
twitter: @hassan
This topic is locked and can not be replied to.