在jruby中实现自定义java类

时间:2010-07-01 20:10:52

标签: java jruby

我正在尝试从jRuby中的Stanford NLP Parser

实现一个java类的集合

我能够在jRuby中实现常规Java,但不能在Stanford Parser类中实现

#my requires/imports/includes, included multiple versions to be safe
require 'java'
include Java
require 'c:\\stanford-parser\\stanfordparser.jar'
require 'C:\\Stanford-Parser\\Current\\stanford-parser.jar'
require 'c:\\sun\\stanfordparser'
require 'rubygems'
include_class 'edu.stanford.nlp.parser.lexparser.LexicalizedParser'


#try to create an object of the java class i am importing, LexicalizedParser
lp = edu.stanford.nlp.parser.lexparser.LexicalizedParser
#the line above is what causes the error

#check if regular Java is working
list = java.util.ArrayList.new 
a = "1"
b = "2"
list.add(a)
list.add(b)
d = list[0]
puts d # all of this works

我收到此错误

~\rubyjavatest\lib\main.rb:15: undefined local variable or method `edu' for main:Object (NameError)

(〜表示我删除了缩短此内容的整条路径)

如果我试试这个:

lp = java::edu::stanford::nlp.parser::lexparser::LexicalizedParser

我收到此错误

~\rubyjavatest\lib\main.rb:15: cannot load Java class java.edu.stanford.nlp.parser.lexparser.LexicalizedParser (NameError)

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

试试这个:lp = LexicalizedParser.new

您需要像使用ArrrayList一样调用new。此外,在致电include_class后,您无需列出完全限定的类名。

我对Stanford NLP Parser不熟悉,所以我认为这样可行。可能需要将其他参数传递给构造函数。