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

02ee2ff9234924331adcb4845754e75e?d=identicon&s=25 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
getting
the following error ...

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

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

*ERROR :*

$ ruby java-ruby.rb
/usr/local/Cellar/ruby/2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require': cannot load such file -- j*ava (LoadError)*
 from
/usr/local/Cellar/ruby/2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
from java-ruby.rb:1:in `<main>'
mselv2m1:ruby_Code mselv2$

Thanks ,
Muthu Selvan SR
0dae30569556c4f47a5277e8957f1960?d=identicon&s=25 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,
not
JRuby.

Mark


On Sat, Jun 22, 2013 at 4:21 PM, Muthu Selvan SR
<pearl.selvan@gmail.com>wrote:

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


--
E: mark.mandel@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

2 Devs from Down Under Podcast
http://www.2ddu.com/
02ee2ff9234924331adcb4845754e75e?d=identicon&s=25 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
486ca04f06d968004643ce5b47376ded?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
http://about.me/keithrbennett
02ee2ff9234924331adcb4845754e75e?d=identicon&s=25 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
command
output for successful installation in jruby

*$ jruby -S gem list*

*** LOCAL GEMS ***

awesome_print (1.1.0)
blankslate (2.1.2.4)
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 (1.2.11.6)
rubygems-bundler (1.2.0)
rubygems-update (2.0.3)
rubyXL (1.2.10)
run_loop (0.0.15, 0.0.12)
rvm (1.11.3.8)
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"*
1
3


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

Thanks ,
Muthu Selvan SR
C983ad33b47479f17a28e083e1bb3d8b?d=identicon&s=25 Eric West (edubkendo)
on 2013-06-23 20:33
(Received via mailing list)
@Muthu, just like you ran $ *jruby -e "puts [1,2,3].minmax" , *  in
order
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
to
be already installed on your system and on your path. There are many
ways
to get around this, from using a version manager such as RVM or rbenv,
to
managing your path yourself, to aliasing ruby to the jruby executable
(which I am NOT reccomending), but to simply run your script, you need
to
actually run the jruby executable instead of the MRI ruby one which
appears
to already be on your path .
cheers
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 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
<pearl.selvan@gmail.com> wrote:

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

>>>> $ ruby java-ruby.rb
>>>>
>>>>
/usr/local/Cellar/ruby/2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
>>>> `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 ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.