从JRuby调用Java类时出现问题 [英] Problems Calling a Java Class from JRuby

查看:57
本文介绍了从JRuby调用Java类时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JRuby中的 boilerpipe .我已经看过指南,它可以从JRuby调用Java,并已成功地将其与其他Java一起使用Java程序包,但无法弄清楚为什么同一事物不能与样板程序一起使用.

I'm trying to use boilerpipe from JRuby. 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相同的工作:

I'm trying to basically do the equivalent of this Java from JRuby:

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

在JRuby中对此进行了尝试:

Tried this in JRuby:

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

这基于 API Javadocs 用于锅炉.这是错误:

This is based on the API Javadocs for boilerpipe. 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)'

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

Looks like it didn't parse the camelcase into the appropriate Java package name. 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:

$ 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

推荐答案

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

I'd recommend against trying to guess the module name we put under Java::, since for unusual packages it can get mangled pretty badly. 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.

此外,由于您可能会遇到此问题...您将需要将INSTANCE变量引用为ArticleExtractor :: INSTANCE,因为我们将其映射为Ruby常量.

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.

玩得开心!

这篇关于从JRuby调用Java类时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆