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

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:inrequire’
from java-ruby.rb:1:in `’
mselv2m1:ruby_Code mselv2$

Thanks ,
Muthu S. SR

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 S. SR
[email protected]wrote:

require ‘java’

Thanks ,
Muthu S. SR


E: [email protected]
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

2 Devs from Down Under Podcast
http://www.2ddu.com/

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 S. SR

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

On Sun, Jun 23, 2013 at 11:14 AM, Muthu S. SR
[email protected] 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 S. ------------------------ [email protected]

twitter: @hassan

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 S. SR