Now for the ruby part. To review; in part one of the Ruby in Java with Maven series, we built a simple maven project which executes java 6 code and fires off a simple JavaScript using the built in JavaScript script engine. Now let’s try another language. If you recall from part 1, the only supported engine was the “Mozilla Rhino” engine which is the built in JavaScript engine. In order to run ruby, we’re going to have to add support for JRuby.

To do this, we’re going to need an implementation of the JRuby scripting engine. Fortunately the kind folks at the java scripting project have already implemented a bunch of scripting engines including JRuby. We just need to download the project, extract the necessary jar and import it into our maven repository to be included in our project. Here’s how…

  • Download the jsr223-engines project from the project downloads page
  • Extract the file to a local directory
  • cd to jsr223-engines/jruby/build
  • Execute the following command to import the lib to your maven repository

$ mvn install:install-file -Dfile=jruby-engine.jar -DgroupId=org.jruby -DartifactId=jruby-engine -Dversion=1.1 -Dpackaging=jar

Once the Jruby engine implementation is installed into your maven repository, you can include both the script engine and the Jruby implementation in your project. Edit your pom.xml to include the following 2 dependencies:

      ...

            org.jruby
            jruby
            1.1
            compile

            org.jruby
            jruby-engine
            1.1
            compile

Wow that was easy. We must have passed the inflection point where maven starts to become useful. If you want to verify that the ruby engine is now supported, you can rerun the code we wrote in part 1 that lists the supported engines.

Now lets write our awesome Ruby script. Create the directory src/main/ruby and store your script there.

# hello_world.rb
# this is a very complex ruby script
puts "Hello world"

Now modify your java to call the ruby script:

package com.weromans;

import java.io.FileReader;
import java.io.FileNotFoundException;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class App
{
    public static void main( String[] args ) throws
                                             FileNotFoundException,
                                             ScriptException
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine rubyEngine = manager.getEngineByName("jruby");
        rubyEngine.eval(
                         new FileReader("src/main/ruby/hello_world.rb"));
    }
}

Now run it:
$ mvn exec:java
[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Building scripting
[INFO] task-segment: [exec:java]
[INFO] ————————————————————————
[INFO] Preparing exec:java
[INFO] No goals needed for project – skipping
Downloading: http://repo1.maven.org/maven2/org/jruby/jruby-engine/1.1/jruby-engine-1.1.pom
[INFO] Unable to find resource ‘org.jruby:jruby-engine:pom:1.1′ in repository central (http://repo1.maven.org/maven2)
[INFO] [exec:java]
Hello World
[INFO] ————————————————————————
[INFO] BUILD SUCCESSFUL
[INFO] ————————————————————————
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Apr 17 17:25:40 EDT 2009
[INFO] Final Memory: 5M/10M
[INFO] ————————————————————————

As you can see, the Hello world in the above output is from the ruby script. Congratulations, you’re running ruby in the JVM. Let’s take it a step further and pass some data to the ruby script from the Java. First we’ll modify our ruby script to print a variable.

# hello_world.rb
# this is a very complex ruby script
puts "Hello world, my name is #{$myname}"

We are now printing a global variable $myname in our ruby script. Let’s modify the java to pass that variable along to the ruby script.

package com.weromans;

import java.io.FileReader;
import java.io.FileNotFoundException;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.ScriptContext;

public class App
{
    public static void main( String[] args ) throws
                                             FileNotFoundException,
                                             ScriptException
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine rubyEngine = manager.getEngineByName("jruby");

        ScriptContext context = rubyEngine.getContext();
        context.setAttribute("myname", "Matt", ScriptContext.ENGINE_SCOPE);

        rubyEngine.eval(new FileReader("src/main/ruby/hello_world.rb"));
    }
}

Run it again:
$ mvn compile exec:java
[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Building scripting
[INFO] task-segment: [compile, exec:java]
[INFO] ————————————————————————
[INFO] [resources:resources]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:Documents and SettingsmromanMy Documentsworkspacejavascriptingscriptingsrcmainresources
Downloading: http://repo1.maven.org/maven2/org/jruby/jruby-engine/1.1/jruby-engine-1.1.pom
[INFO] Unable to find resource ‘org.jruby:jruby-engine:pom:1.1′ in repository central (http://repo1.maven.org/maven2)
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to c:Documents and SettingsmromanMy Documentsworkspacejavascriptingscriptingtargetclasses
[INFO] Preparing exec:java
[INFO] No goals needed for project – skipping
[INFO] [exec:java]
Hello World my name is Matt
[INFO] ————————————————————————
[INFO] BUILD SUCCESSFUL
[INFO] ————————————————————————
[INFO] Total time: 3 seconds
[INFO] Finished at: Fri Apr 17 17:36:02 EDT 2009
[INFO] Final Memory: 12M/22M
[INFO] ————————————————————————

Nice. You can see your line Hello World my name is Matt in the above output. That was pretty easy.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2012 WeRomans Suffusion theme by Sayontan Sinha