2

我正在尝试通过 JRuby 向 Java 应用程序公开我编写的 ruby​​ gem 的功能。我在让 jruby 加载 bson-1.8.5-java gem 而不是原生扩展 bson_ext 时遇到问题,它不能很好地与 JRuby 配合使用。我的目标是对我的 ruby​​ gem 进行最小的更改,所以我不想在我的 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 gem 组合起来。

$ jruby -S gem install -i ./my-gems mongo --no-rdoc --no-ri
Fetching: bson-1.8.5-java.gem (100%)
Fetching: mongo-1.8.5.gem (100%)
Successfully installed bson-1.8.5-java
Successfully installed mongo-1.8.5
2 gems installed
$ jar cf my-gems.jar -C ./my-gems .

我将 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
...

但是,如果我require 'mongo'从 jirb 运行,它会加载。

任何指针?

4

0 回答 0