简体   繁体   中英

How do I use JRuby and Java?

Is that feature still supported? I can't seem to find any Documentation past 2008 or so. This is an insert from the Oracle website...

At runtime, the JSR 223 Scripting APIs must locate the appropriate script engine for the scripting language you want to use. 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.

But it was written in 2007 and the link for all of these links are no longer in service. So I am unable to get any JRuby engine...anything.

Oracle Doc: 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. I currently have JRuby running successfully, but don't know how to setup Scriptengines in Java.

Thanks Very much.

You can go and download the complete jruby .jar file from here

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:

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

If your idea is to just call Java from Ruby, thats far easy, you can follow the wiki located here

I found current Documentation on the JRuby site. Or rather github/JRuby.

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

This is called 'RedBridge' and is described in detail here. 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

Which is expected.

Thanks rorra for the jruby-complete.jar link, I copied the .jar into my java lib folder and imported it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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