如何从JRuby生成的Java类中使用Mongo?

如何从JRuby生成的Java类中使用Mongo?,java,ruby,jruby,Java,Ruby,Jruby,我试图公开我通过JRuby编写到Java应用程序的RubyGem的功能。我在让jruby加载bson-1.8.5-java gem而不是本机扩展bson_ext时遇到了一个问题,因为本机扩展bson_ext与jruby不兼容。我的目标是对我的RubyGem进行最小的更改,所以我不希望在ruby代码中导入Java类 将我的ruby文件编译成Java类后,它们将调用require'mongo' public class Goose extends RubyObject { private

我试图公开我通过JRuby编写到Java应用程序的RubyGem的功能。我在让jruby加载bson-1.8.5-java gem而不是本机扩展bson_ext时遇到了一个问题,因为本机扩展bson_ext与jruby不兼容。我的目标是对我的RubyGem进行最小的更改,所以我不希望在ruby代码中导入Java类

将我的ruby文件编译成Java类后,它们将调用
require'mongo'

public class Goose extends RubyObject  {
    private static final Ruby __ruby__ = Ruby.getGlobalRuntime();
private static final RubyClass __metaclass__;

    static {
        String source = new StringBuilder("require 'mongo'\n" +
            "\n" +
            "class Goose\n" +
            "  def initialize\n" +
        ...
    __ruby__.executeScript(source, "goose.rb");
        ...
从Java调用类失败,并抱怨没有加载BSON扩展

这个问题可以归结为

我把mongo和bson的java宝石按照

我将my-gems.jar添加到我的Eclipse项目类路径中

然后我运行了一个简单的main来测试require评估

public static void main(String[] args) {
    System.setProperty("org.jruby.embed.compat.version", "JRuby1.9");
    ScriptEngineManager factory = new ScriptEngineManager();

    ScriptEngine engine = factory.getEngineByName("jruby");

    try {
        engine.eval("require 'mongo'");
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}
哪个输出

      ** Notice: The BSON extension was not loaded. **

  For optimal performance, use of the BSON extension is recommended. To
  enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.

  Exception in thread "main" java.lang.ExceptionInInitializerError at JavaRubyTest.main(JavaRubyTest.java:27)
  Caused by: org.jruby.exceptions.RaiseException: (NameError) cannot load Java class org.bson.types.ObjectId
...
但是,如果我从jirb运行
require'mongo'
,它会加载

有什么建议吗

      ** Notice: The BSON extension was not loaded. **

  For optimal performance, use of the BSON extension is recommended. To
  enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.

  Exception in thread "main" java.lang.ExceptionInInitializerError at JavaRubyTest.main(JavaRubyTest.java:27)
  Caused by: org.jruby.exceptions.RaiseException: (NameError) cannot load Java class org.bson.types.ObjectId
...