I accidentally posted this originally to the Ruby forum instead of JRuby
forum. Sorry.
I’m unable to get a simple ruby script to work that makes use of a Java
class and could use some assistance. I’ve read all through the wiki and
tried some googling, but no luck.
JRuby Version:
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot™ 64-Bit
Server VM 1.7.0_09-b05 [darwin-x86_64]
Java Version:
java version “1.7.0_09”
Java™ SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot™ 64-Bit Server VM (build 23.5-b02, mixed mode)
I’m trying to create a super simple ruby script that interfaces with the
Stanford NLP Parser (Java). For now I’m just trying to recreate, in
Ruby, a the first line from their Java demo. I’ve run the demo and it
runs fine. Here is a snippet of the Java code:
CallableSelector.java:112:in findMatchingCallableForArgs': java.lang.ArrayIndexOutOfBoundsException: 2 from CallableSelector.java:45:inmatchingCallableArityN’
from RubyToJavaInvoker.java:215:in findCallable' from StaticMethodInvoker.java:28:incall’
from StaticMethodInvoker.java:55:in call' from CachingCallSite.java:326:incacheAndCall’
from CachingCallSite.java:170:in call' from blah.rb:6:infile’
from blah.rb:-1:in load' from Ruby.java:770:inrunScript’
from Ruby.java:763:in runScript' from Ruby.java:640:inrunNormally’
from Ruby.java:489:in runFromMain' from Main.java:375:indoRunFromMain’
from Main.java:264:in internalRun' from Main.java:230:inrun’
from Main.java:214:in run' from Main.java:194:inmain’
The jar files should not be passed as args to ‘require’. ‘require’ takes
Ruby scripts, and the interpreter interprets the Ruby source code. It
would therefore make sense that the interpreter would choke on the
binary jar file.
Instead, make sure the jar files are in your path. You can do so by
setting your CLASSPATH environment variable. On Unix/Mac OS you can do
this on the JRuby command line:
jruby -e “require ‘java’; java_import
‘org.apache.commons.lang3.ArrayUtils’”
NameError: cannot load Java class org.apache.commons.lang3.ArrayUtils
for_name at org/jruby/javasupport/JavaClass.java:1206
get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
java_import at
file:/Users/keithb/.rvm/rubies/jruby-1.7.0/lib/jruby.jar!/jruby/java/core_ext/object.rb:26
map at org/jruby/RubyArray.java:2355
java_import at
file:/Users/keithb/.rvm/rubies/jruby-1.7.0/lib/jruby.jar!/jruby/java/core_ext/object.rb:22
(root) at -e:1
Note that the first command worked, while the second (without the
classpath setting) didn’t.
Is there a best practice regarding whether to make a jar file available
via require or via the classpath? Or, otherwise put, when is one better
than the other?
I guess if the jar file is guaranteed to be in the LOAD_PATH (or
somewhere relative to it), then it’s fine to use require, but otherwise
not?
Or, it may be a question of whether to modify the LOAD_PATH to include
the jar file’s directory or the CLASSPATH to include the jar file?
…etc., etc.
Someone in the irc channel had an issue with, if I recall, this same
stanford
nlp parser. The problem was that the libraries being 'require’ed into
JRuby
had version 1.1 (version numbers are made up) of some jar file that
JRuby also
happened to include, only JRuby’s was 1.0 (or 1.2?). So JRuby’s version
was
masking classes in the other version, and this caused the third-party
library
to break. We solved it by adding the third-party jars to the
commandline with
-J-cp, so they took precedence. (At least, that was my understanding of
what
happened. Heck, maybe this is the same person from irc because we
failed to
solve the problem…)
Just throwing that out there since it seems relevant.