我试图通过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扩展。
问题可以归结为
根据this tutorial,我把mongo和bson-java宝石搞得一团糟。
$ 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项目类路径中。
然后我运行了一个简单的主程序来测试需求评估
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'
,则会加载。
任何指针?