简体   繁体   English

如何使用JRuby和Java?

[英]How do I use JRuby and Java?

Is that feature still supported? 仍然支持该功能吗? I can't seem to find any Documentation past 2008 or so. 我似乎找不到2008年左右以后的任何文档。 This is an insert from the Oracle website... 这是Oracle网站上的内容...

At runtime, the JSR 223 Scripting APIs must locate the appropriate script engine for the scripting language you want to use. 在运行时,JSR 223脚本API必须为您要使用的脚本语言找到合适的脚本引擎。 The script engine interprets and executes the script. 脚本引擎解释并执行脚本。 You can get the current JSR 223 third-party script engines from the Scripting Project on java.net by downloading the jsr223-engines.tar.gz or the jsr223-engines file and expanding it somewhere on your system, for example, in the scriptengines directory. 您可以通过下载jsr223-engines.tar.gz或jsr223-engines文件并将其扩展到系统中的某个位置(例如在scriptengines中),从java.net上的Scripting Project中获得当前的JSR 223第三方脚本引擎。目录。

But it was written in 2007 and the link for all of these links are no longer in service. 但是它写于2007年,所有这些链接的链接不再可用。 So I am unable to get any JRuby engine...anything. 所以我什么都无法得到任何JRuby引擎。

Oracle Doc: http://www.oracle.com/technetwork/articles/dsl/jruby-141877.html Oracle文档: http//www.oracle.com/technetwork/articles/dsl/jruby-141877.html

If anyone could supply me with a current Tutorial or Docs that I can Use JRuby inside Java. 如果有人可以向我提供最新的教程或文档,那么我可以在Java中使用JRuby。 I currently have JRuby running successfully, but don't know how to setup Scriptengines in Java. 目前,我已经成功运行了JRuby,但不知道如何在Java中设置脚本引擎。

Thanks Very much. 非常感谢。

You can go and download the complete jruby .jar file from here 您可以从此处下载完整的jruby .jar文件

Then I just grabbed some sample code: 然后,我获取了一些示例代码:

import org.jruby.*;
import org.jruby.javasupport.JavaEmbedUtils;  
import java.util.ArrayList;

public class RubyFromJava {
    public String fooString() {
        return "foo";
    }

    public static void main(String[] ARGV) {
        System.out.println("Started");
        RubyFromJava sTest = new RubyFromJava();

        System.out.println("boring: " + sTest.fooString() + "\n"); // outputs foo

        /* This script subclasses the java Object class.
        It is then instantiated and the foobarString method is called. */

        String script = 
            "require 'java'\n" +
            "class RSubclass1 < java.lang.Object\n" + // subclassing java.* is magic
            "   def foobarString\n" +
            "       return @returnString = toString() + 'BAR'\n" +
            "   end\n" +
            "end\n" +
            "rsubclass = RSubclass1.new\n" +
            "puts rsubclass.foobarString\n";

    Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
    RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();

    evaler.eval(runtime, script); // outputs org.jruby.proxy.java.lang.Object$Proxy0@1c7f37dBAR

        System.out.println("-----------------\n");

        /* This script subclasses the RubyFromJava class.
        It is then instantiated and the foobarString method is called. */

        script = 
            "require 'java'\n" +
                        "class RSubclass2 < Java::RubyFromJava\n" + // subclassing non {org,com}.* classpath requires you prefix the class with Java:: or java.
            "   def foobarString\n" +
            "       return @returnString = fooString() + 'BAR'\n" +
            "   end\n" +
            "\n" +
            "rsubclass = RSubclass2.new\n" +
            "puts(rsubclass.foobarString())\n" +
            "end";
                 evaler.eval(runtime, script); // outputs fooBAR
    }
}

saved it as RubyFromJava.java , I have both RubyFromJava.java and jruby-complete-9.0.0.0.rc1.jar on the same dir and ran: 将其保存为RubyFromJava.java ,我在相同的目录下同时拥有RubyFromJava.javajruby-complete-9.0.0.0.rc1.jar并运行:

rorra:~ > javac -cp ./jruby-complete-9.0.0.0.rc1.jar RubyFromJava.java
Note: RubyFromJava.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

rorra:~ > java -cp ./jruby-complete-9.0.0.0.rc1.jar:. RubyFromJava
Started
boring: foo

org.jruby.proxy.java.lang.Object$Proxy1@273e5037BAR
-----------------

fooBAR

works as expected and invoked jruby code from java 按预期工作并从Java调用jruby代码

If your idea is to just call Java from Ruby, thats far easy, you can follow the wiki located here 如果您的想法是仅从Ruby调用Java,那很容易,您可以按照此处的Wiki进行操作

I found current Documentation on the JRuby site. 我在JRuby站点上找到了当前文档。 Or rather github/JRuby. 还是github / JRuby。

https://github.com/jruby/jruby/wiki/RedBridge https://github.com/jruby/jruby/wiki/RedBridge

This is called 'RedBridge' and is described in detail here. 这称为“ RedBridge”,在此进行详细说明。 I have it up and running. 我启动并运行它。

Here is my Sample code: 这是我的示例代码:

import org.jruby.embed.ScriptingContainer;

public class RubyScript {

private RubyScript() {
    int add = 5;
    //initializes Ruby Environment
    ScriptingContainer container = new ScriptingContainer();
    //Sets Ruby var 'ans' as java.lang.Integer: add
    container.put("ans",add);
    container.runScriptlet("puts ans");
    System.out.println(add + add);
}

public static void main(String[] args) {
    new RubyScript();
}
}

This outputs: 5 10 输出:5 10

Which is expected. 这是预期的。

Thanks rorra for the jruby-complete.jar link, I copied the .jar into my java lib folder and imported it. 感谢rorra提供的jruby-complete.jar链接,我将.jar复制到了我的java lib文件夹中并导入了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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