简体   繁体   English

从JRuby调用Java类的问题

[英]Problems Calling a Java Class from JRuby

I'm trying to use boilerpipe from JRuby. 我正在尝试使用JRuby的samppipe I've seen the guide for calling Java from JRuby, and have used it successfully with another Java package, but can't figure out why the same thing isn't working with boilerpipe. 我已经看过从JRuby调用Java的指南 ,并且已经成功地将它用于另一个Java包,但是无法弄清楚为什么同样的东西不能用于samppipe。

I'm trying to basically do the equivalent of this Java from JRuby: 我试图基本上从JRuby做相当于这个Java:

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

Tried this in JRuby: 在JRuby尝试过这个:

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

This is based on the API Javadocs for boilerpipe. 这是基于用于samppipe的API Javadocs Here's the error: 这是错误:

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)'

Looks like it didn't parse the camelcase into the appropriate Java package name. 看起来它没有将camelcase解析为适当的Java包名称。 What am I doing wrong? 我究竟做错了什么? I believe I've set up my classpath alright (last 3 entries), though there may be some conflict with xerces possibly being included twice: 我相信我已经设置了我的类路径(最后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

I'd recommend against trying to guess the module name we put under Java::, since for unusual packages it can get mangled pretty badly. 我建议不要试图猜测我们放在Java ::下的模块名称,因为对于不寻常的软件包,它可能会被严重破坏。 Use java_import 'your.weird.package.ArticleExtractor' or if all the package components are compatible with Ruby method naming, you can also do Java::your.weird.package.ArticleExtractor. 使用java_import'your.weird.package.ArticleExtractor'或者如果所有包组件都与Ruby方法命名兼容,您也可以使用Java :: your.weird.package.ArticleExtractor。

Also, since you might run into this... you'll want to reference the INSTANCE variable as ArticleExtractor::INSTANCE, since we map it as a Ruby constant. 此外,由于您可能遇到此问题...您将要将INSTANCE变量引用为ArticleExtractor :: INSTANCE,因为我们将其映射为Ruby常量。

Have fun! 玩得开心!

You can also use the nice Jruby Boilerpipe Gem which wraps the Java code 你也可以使用包装Java代码的漂亮的Jruby Boilerpipe Gem

Or the pure ruby implementation of Boilerpipe Ruby Boilerpipe Gem 或者是Boilerpipe Ruby Boilerpipe Gem的纯ruby实现

require 'boilerpipe'  

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM