implementing custom java class in jruby

孤者浪人 提交于 2019-12-11 03:34:08

问题


I am trying to implement a collection of java classes from the Stanford NLP Parser in jRuby

I am able to implement regular Java in jRuby, but not the Stanford Parser classes

#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

I get this error

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

(the ~ represents I cut out the whole path to shorten this)

also if I try this:

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

I get this error

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

Any help would be great!


回答1:


Try this: lp = LexicalizedParser.new

You need to call new like you did with ArrrayList. Also, you shouldn't need to list the fully qualified class name after you call include_class.

I am not familiar with the Stanford NLP Parser, so I am assuming this will work. It might be necessary to pass additional parameters to the constructor.



来源:https://stackoverflow.com/questions/3161312/implementing-custom-java-class-in-jruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!