简体   繁体   中英

Issues loading java class into gem in JRuby

I am trying to add JRuby support to my C-extension gem, by writing a Java class and executing it when the gem is installed on JRuby. The code in question lives on this branch , with the important files being the java class and the code to load it when used in JRuby

At this point, I am just trying to ensure I have my gem configured correctly, and fully expect it to fail the specs.

When I try to run rspec (or the gem in IRB for that matter) I get the following backtrace

NameError: cannot load Java class com.dockyard.PgArrayParser
  get_proxy_or_package_under_package at org/jruby/javasupport/JavaUtilities.java:54
                      method_missing at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/shared/builtin/javasupport/java.rb:51
                       PgArrayParser at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:6
                              (root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:3
                             require at org/jruby/RubyKernel.java:1033
                             require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
                              (root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:2
                             require at org/jruby/RubyKernel.java:1033
                             require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
                              (root) at /Users/dan/Projects/dockyard/pg_array_parser/lib/pg_array_parser.rb:1
                             require at org/jruby/RubyKernel.java:1033
                             require at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
                              (root) at /Users/dan/Projects/dockyard/pg_array_parser/spec/spec_helper.rb:1
                                load at org/jruby/RubyKernel.java:1058
                     load_spec_files at /Users/dan/Projects/dockyard/pg_array_parser/spec/parser_spec.rb:746
                             collect at org/jruby/RubyArray.java:2331
                     load_spec_files at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746
                                 run at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22
                                 run at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69
                             autorun at /Users/dan/.rbenv/versions/jruby-1.6.7.2/lib/ruby/gems/1.8/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10
                                call at org/jruby/RubyProc.java:270
                                call at org/jruby/RubyProc.java:224

Having recently worked on a JRuby extension myself, here are some of the gotchas which I have found through experiment -- and which seem to cause you problems too:

  • The java package must match the Ruby module in which your class sits. If your class is in the PgArrayParser module, your class must be in the pgarrayparser (all lowercase).
  • If you want to instantiate a PgArrayParserEngine class, then you BasicLibraryService must be called PgArrayParserEngineService .
  • When using @JRubyMethod , specify the name of the method.

I have found Yoko's article on the subject most useful, along with this recent post .

I have applied these different elements to your project here , and I can call the method from IRB -- and the first spec passes!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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