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(TM) 64-Bit
Server VM 1.7.0_09-b05 [darwin-x86_64]
Java Version:
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 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:
-----------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.io.StringReader;
import edu.stanford.nlp.objectbank.TokenizerFactory;
import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.process.PTBTokenizer;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class ParserDemo {
public static void main(String[] args) {
LexicalizedParser lp =
LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
...
-----------------------------
Here is my simple jruby script:
-----------------------------
require './stanford-parser.jar'
require './stanford-parser-models.jar'
java_import "edu.stanford.nlp.parser.lexparser.LexicalizedParser"
lp =
LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
puts "Done"
-----------------------------
I get this error. Any help is appreciated.
CallableSelector.java:112:in `findMatchingCallableForArgs':
java.lang.ArrayIndexOutOfBoundsException: 2
from CallableSelector.java:45:in `matchingCallableArityN'
from RubyToJavaInvoker.java:215:in `findCallable'
from StaticMethodInvoker.java:28:in `call'
from StaticMethodInvoker.java:55:in `call'
from CachingCallSite.java:326:in `cacheAndCall'
from CachingCallSite.java:170:in `call'
from blah.rb:6:in `__file__'
from blah.rb:-1:in `load'
from Ruby.java:770:in `runScript'
from Ruby.java:763:in `runScript'
from Ruby.java:640:in `runNormally'
from Ruby.java:489:in `runFromMain'
from Main.java:375:in `doRunFromMain'
from Main.java:264:in `internalRun'
from Main.java:230:in `run'
from Main.java:214:in `run'
from Main.java:194:in `main'
Thanks!
on 2012-10-27 23:51
on 2012-10-28 00:12
Lab Goat (love the name) -
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:
[1] 18:09 austin-powers /Users/keithb/temp/commons-lang3-3.1
>CLASSPATH=./commons-lang3-3.1.jar jruby -e "require 'java';
java_import 'org.apache.commons.lang3.ArrayUtils'"
[0] 18:09 austin-powers /Users/keithb/temp/commons-lang3-3.1
>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.
- Keith
Keith R. Bennett
http://about.me/keithrbennett
on 2012-10-28 19:10
Hi, Le 27/10/2012 23:11, Keith Bennett a écrit : > Lab Goat (love the name) - > > The jar files should not be passed as args to 'require'. Sure you can: require 'commons-lang3-3.1.jar' java_import 'org.apache.commons.lang3.StringUtils' StringUtils::split_by_character_type_camel_case("JRubyRules").each do |c| puts c end If the jar is on the LOAD_PATH, then jruby will load it. (You also don’t need to require 'java' any longer in JRuby 1.7, I don’t know if this is an “official” feature! ;) ) OP’s code could thus become: require 'stanford-parser.jar' java_import 'edu.stanford.nlp.parser.lexparser.LexicalizedParser' lp = LexicalizedParser::load_model_from_zip('stanford-parser-models.jar', 'edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz') puts "Done" Regards, Sébastien. 'require' takes
on 2012-10-28 20:38
Sébastien - I stand corrected. I found this link that provides more information about using require on a jar file: http://kenai.com/projects/jruby/pages/CallingJavaF... 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. Thanks, Keith Keith R. Bennett http://about.me/keithrbennett
on 2012-10-28 22:59
Hi, Le 28/10/2012 19:37, Keith Bennett a écrit : > 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. > See https://github.com/jruby/jruby/wiki/ClasspathAndLoadPath I personally prefer using LOAD_PATH: it feels more ruby-ish. Regards, Sébastien.
on 2012-10-29 02:34
> require 'stanford-parser.jar' > java_import 'edu.stanford.nlp.parser.lexparser.LexicalizedParser' 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.