2011-03-22 62 views
5

我正在嘗試使用JRuby的boilerpipe。我已經看到了從JRuby調用Java的guide,並且已經成功地將它用於另一個Java包,但無法弄清楚爲什麼同樣的東西不能用於boilerpipe。從JRuby調用Java類的問題

我試圖從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) 

這是基於對API Javadocs boilerpipe。以下是錯誤:

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 

回答

9

我建議你不要試圖去猜測,我們把下面的模塊名稱Java ::,因爲對於不尋常的軟件包,它可能會變得非常糟糕。使用java_import'your.weird.package.ArticleExtractor',或者如果所有包組件都與Ruby方法命名兼容,則還可以執行Java :: your.weird.package.ArticleExtractor。

此外,由於您可能會遇到這種情況......您將需要將INSTANCE變量作爲ArticleExtractor :: INSTANCE引用,因爲我們將其映射爲Ruby常量。

玩得開心!

+0

由於查爾斯的純Ruby實現,工作就像一個魅力! – ehsanul 2011-03-22 19:35:19

+0

以「de.l3s」開頭的軟件包的道歉。 L3S是我在開發鍋爐管庫時所使用的公司/研究中心的名稱[http://www.L3S.de/](http://www.L3S.de/) – 2011-03-22 19:36:42

+2

您必須愛上StackOverflow。問一個關於從JRuby使用庫的問題,您將獲得JRuby的首席開發人員和庫的創建者回答! (在提問的6分鐘內,不少於) – 2011-03-22 20:15:41

0

你也可以用好的Jruby Boilerpipe Gem包裝了Java代碼

或者Boilerpipe Ruby Boilerpipe Gem

require 'boilerpipe' 

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