java jruby,在jruby中实现自定义Java类

作者在尝试在JRuby环境中使用Stanford NLP解析器时遇到问题,导入并实例化LexicalizedParser类时出现错误。已经尝试了多种jar包引入方式和全限定类名,但仍然失败。常规的Java代码如ArrayList工作正常。解决方案可能是调用类的构造函数,例如`lp = LexicalizedParser.new`,可能需要传递额外参数。
摘要由CSDN通过智能技术生成

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!

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值