从JRuby调用Java类的问题

时间:2011-03-22 19:00:22

标签: java ruby jruby

我正在尝试使用JRuby中的boilerpipe。我已经看到guide用于从JRuby调用Java,并且已经成功地将它用于另一个Java包,但是无法弄清楚为什么同样的东西不能用于samppipe。

我试图从JRuby基本上做相当于这个Java:

URL url = new URL("http://www.example.com/some-location/index.html");
String text = ArticleExtractor.INSTANCE.getText(url);

在JRuby中试过这个:

require 'java'
url = java.net.URL.new("http://www.example.com/some-location/index.html")
text = Java::DeL3sBoilerpipeExtractors::ArticleExtractor.INSTANCE.getText(url)

这是基于samppipe的API Javadocs。这是错误:

jruby-1.6.0 :042 > Java::DeL3sBoilerpipeExtractors::ArticleExtractor
NameError: cannot load Java class deL3sBoilerpipeExtractors.ArticleExtractor
        from org/jruby/javasupport/JavaClass.java:1195:in `for_name'
        from org/jruby/javasupport/JavaUtilities.java:34:in `get_proxy_class'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:45:in `const_missing'
        from (irb):42:in `evaluate'
        from org/jruby/RubyKernel.java:1087:in `eval'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:158:in `eval_input'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:271:in `signal_status'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:270:in `signal_status'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:155:in `eval_input'
        from org/jruby/RubyKernel.java:1417:in `loop'
        from org/jruby/RubyKernel.java:1190:in `catch'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:154:in `eval_input'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:71:in `start'
        from org/jruby/RubyKernel.java:1190:in `catch'
        from /usr/local/rvm/rubies/jruby-1.6.0/lib/ruby/1.8/irb.rb:70:in `start'
        from /usr/local/rvm/rubies/jruby-1.6.0/bin/irb:17:in `(root)'

看起来它没有将camelcase解析为适当的Java包名称。我究竟做错了什么?我相信我已经设置了我的类路径(最后3个条目),尽管可能存在一些与xerces可能被包含两次的冲突:

$ echo $CLASSPATH
:/jellly/Maui1.2:/jellly/Maui1.2/src:/jellly/Maui1.2/bin:/jellly/Maui1.2/lib/commons-io-1.4.jar:/jellly/Maui1.2/lib/commons-logging.jar:/jellly/Maui1.2/lib/icu4j_3_4.jar:/jellly/Maui1.2/lib/iri.jar:/jellly/Maui1.2/lib/jena.jar:/jellly/Maui1.2/lib/maxent-2.4.0.jar:/jellly/Maui1.2/lib/mysql-connector-java-3.1.13-bin.jar:/jellly/Maui1.2/lib/opennlp-tools-1.3.0.jar:/jellly/Maui1.2/lib/snowball.jar:/jellly/Maui1.2/lib/trove.jar:/jellly/Maui1.2/lib/weka.jar:/jellly/Maui1.2/lib/wikipediaminer1.1.jar:/jellly/Maui1.2/lib/xercesImpl.jar:/jellly/boilerpipe-1.1.0/boilerpipe-1.1.0.jar:/jellly/boilerpipe-1.1.0/lib/nekohtml-1.9.13.jar:/jellly/boilerpipe-1.1.0/lib/xerces-2.9.1.jar

2 个答案:

答案 0 :(得分:9)

我建议不要试图猜测我们放在Java ::下的模块名称,因为对于不寻常的包,它可能会被严重损坏。使用java_import'your.weird.package.ArticleExtractor'或者如果所有的包组件都与Ruby方法命名兼容,你也可以使用Java :: your.weird.package.ArticleExtractor。

另外,既然你可能会碰到这个......你会想要将INSTANCE变量引用为ArticleExtractor :: INSTANCE,因为我们将它映射为Ruby常量。

玩得开心!

答案 1 :(得分:0)

您还可以使用包装Java代码的nice Jruby Boilerpipe Gem

或者Boilerpipe Ruby Boilerpipe Gem

的纯ruby实现
require 'boilerpipe'  

Boilerpipe::Extractors::ArticleExtractor.text("https://github.com/jruby/jruby/wiki/AboutJRuby")