簡體   English   中英

如何使用JRuby和Java?

[英]How do I use JRuby and Java?

仍然支持該功能嗎? 我似乎找不到2008年左右以后的任何文檔。 這是Oracle網站上的內容...

在運行時,JSR 223腳本API必須為您要使用的腳本語言找到合適的腳本引擎。 腳本引擎解釋並執行腳本。 您可以通過下載jsr223-engines.tar.gz或jsr223-engines文件並將其擴展到系統中的某個位置(例如在scriptengines中),從java.net上的Scripting Project中獲得當前的JSR 223第三方腳本引擎。目錄。

但是它寫於2007年,所有這些鏈接的鏈接不再可用。 所以我什么都無法得到任何JRuby引擎。

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

如果有人可以向我提供最新的教程或文檔,那么我可以在Java中使用JRuby。 目前,我已經成功運行了JRuby,但不知道如何在Java中設置腳本引擎。

非常感謝。

您可以從此處下載完整的jruby .jar文件

然后,我獲取了一些示例代碼:

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
    }
}

將其保存為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

按預期工作並從Java調用jruby代碼

如果您的想法是僅從Ruby調用Java,那很容易,您可以按照此處的Wiki進行操作

我在JRuby站點上找到了當前文檔。 還是github / JRuby。

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

這稱為“ RedBridge”,在此進行詳細說明。 我啟動並運行它。

這是我的示例代碼:

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();
}
}

輸出:5 10

這是預期的。

感謝rorra提供的jruby-complete.jar鏈接,我將.jar復制到了我的java lib文件夾中並導入了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM