使用rvm jruby install将JRuby嵌入Java代码中 [英] Embedding JRuby in Java code using rvm jruby install

查看:88
本文介绍了使用rvm jruby install将JRuby嵌入Java代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java应用程序中嵌入和评估ruby代码.而不是将jruby-complete.jar放在我的类路径中,我需要能够使用随rvm安装的jruby环境.我可以执行基本的内核代码,但是遇到需要标准库(fileutils,tmpdir等)的问题.

I'm trying to embed and evaluate ruby code from within a Java application. Instead of putting jruby-complete.jar in my classpath, I need to be able to use jruby environment that's installed with rvm. I can execute basic kernel code, but I'm having issues requiring standard libraries (fileutils, tmpdir, etc.).

我在下面创建了使用通过RVM安装的JRuby的测试文件,如果您具有本地rvm + jruby安装(将JRUBY_VERSION更改为已安装的版本),则任何人都应该能够编译+运行该文件.我意识到我所引用的jruby.jar与jruby-complete.jar不同,但是我希望有一种无需下载外部jar即可加载标准库的方法.

I created the test file below that uses a JRuby installed via RVM, anyone should be able to compile+run it if you have a local rvm + jruby installation (change JRUBY_VERSION to a version installed). I realize the jruby.jar I'm referencing isn't the same as jruby-complete.jar, but I'm hoping there is a way to load standard libraries without downloading an external jar.

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test {
    private final static Logger LOG = Logger.getAnonymousLogger();
    private final static String JRUBY_VERSION = "jruby-1.6.7";

    public static void main(String[] args) throws Throwable {
        final String rvmPath = System.getenv("HOME") + "/.rvm/rubies/";
        addFileToClasspath(rvmPath + JRUBY_VERSION + "/lib/jruby.jar");

        final ScriptEngine rubyEngine = new ScriptEngineManager().getEngineByName("jruby");
        rubyEval(rubyEngine, "puts 'hello world!'"); // works
        rubyEval(rubyEngine, "require 'tempfile'");  // fails to load 'tmpdir'
        rubyEval(rubyEngine, "require 'fileutils'"); // fails to load 'fileutils'
    }

    private static void rubyEval(ScriptEngine rubyEngine, final String code) {
        try {
            rubyEngine.eval(code);
        } catch (final Throwable e) { LOG.throwing(Test.class.getName(), "rubyEval", e); };
    }

    public static void addFileToClasspath(final String path) throws Throwable {
        final File file = new File(path);
        final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        final Class<?> sysclass = URLClassLoader.class;
        final Method method = sysclass.getDeclaredMethod("addURL", new Class<?>[] {URL.class});
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] {file.toURI().toURL()});
    }
}

推荐答案

在rvm安装中,将核心ruby文件与jruby.jar一起安装(对于JRuby 1.7.2,此处是):

On an rvm installation, the core ruby files are installed alongside the jruby.jar (here for JRuby 1.7.2):

sebastien@greystones:~$ cd .rvm/rubies/jruby-1.7.2/
sebastien@greystones:~/.rvm/rubies/jruby-1.7.2$ find . -name fileutils.rb
./lib/ruby/1.8/fileutils.rb
./lib/ruby/1.9/fileutils.rb
sebastien@greystones:~/.rvm/rubies/jruby-1.7.2$ find . -name tmpdir.rb
./lib/ruby/1.8/tmpdir.rb
./lib/ruby/1.9/tmpdir.rb

因此,您需要向类路径中添加正确的ruby文件夹(取决于ruby版本),例如:

You therefore need to add the correct ruby folder (which depends on the ruby version) to the classpath, for example:

    final String rvmPath = System.getenv("HOME") + "/.rvm/rubies/";
    addFileToClasspath(rvmPath + JRUBY_VERSION + "/lib/jruby.jar");

    // Here, add folder to classpath.
    System.setProperty("org.jruby.embed.class.path", rvmPath + JRUBY_VERSION + "/lib/ruby/1.9");

这篇关于使用rvm jruby install将JRuby嵌入Java代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆