A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/jruby/jruby/wiki/JavaIntegration below:

JavaIntegration · jruby/jruby Wiki · GitHub

» JRuby Project Wiki Home Page

Using the JRuby Interpreter from Java

Most of the information here has been superceded by the RedBridge page. Left here for historic sake, or in case it has clearer examples :)

Java 6 (using JSR 223: Scripting)

Java integration with Java 6 will be using the standard scripting API (JSR223). A JRuby scripting engine already exists and is located at https://scripting.dev.java.net/.

  1. Download and unzip the collection of jars from the documents and files section of the site (jsr223-engines.tar.gz or jsr223-engines.zip).
  2. Look in the uncompressed files for the jruby/build/jruby-engine.jar file.
  3. Add this file to your classpath and then use the code below to access the engine.
    If you're using JRuby 1.1RC3 and 1.1.x on Java 6, use version 1.1.2 or later of the JRuby engine.
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
{...}
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName("jruby");
ScriptContext context = rubyEngine.getContext();

context.setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE);

try{
    rubyEngine.eval("puts 2 + $label", context);
} catch (ScriptException e) {
    e.printStackTrace();
}

ScriptEngine.eval also takes a java.io.Reader object, which allows you to get load scripts from Files or other resource streams very simply, through the same interface. The context parameter is optional.

If you want to use the scripting API on Java 5, use version 1.1.3 or later of the JRuby engine. Additionally, download sjp-1_0-fr-ri.zip from http://www.jcp.org/en/jsr/detail?id=223, then unzip it and add script-api.jar to your classpath.

Java 5 users might want to use com.sun.script.jruby.JRubyScriptEngineManager instead of javax.script.ScriptEngineManager to avoid version mismatch errors. However, if you are sure that you don't have any other script engines' archives compiled on JDK 1.6, you can use javax.script.ScriptEngingManager to get the engine's instance.

If you can't use Java 6, you can use the Apache Bean Scripting Framework.

When running the compiled code, be sure to use a java invocation similar to the following:

 java -cp .:scripts:bsf.jar:jruby.jar:jruby-engine.jar -Djruby.home=/path/to/jruby/home  my.class.ScriptRunner

The -Djruby.home part is necessary so the system can find the ruby libraries.

See Walkthroughs and Tutorials: JSR 223 scripting for more information.

Embedding with Bean Scripting Framework (BSF)

The Bean Scripting Framework, when used with JRuby, will allow you to conveniently to pass your own Java objects to your JRuby script. You can then use these objects in JRuby, and changes will affect your Java program directly. To run a JRuby script using BSF, you must first copy the BSF.jar file into your JAVA_HOME/lib/ext/ folder. Then, try the following:

import org.jruby.Ruby.*;
import org.jruby.*;
import org.jruby.javasupport.bsf.*;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
{...}
JLabel mylabel = new JLabel();
BSFManager.registerScriptingEngine("ruby", 
                                   "org.jruby.javasupport.bsf.JRubyEngine", 
                                   new String[] { "rb" });

BSFManager manager = new BSFManager();

/* Import an object using declareBean then you can access it in JRuby with $<name> */
 
manager.declareBean("label", mylabel, JFrame.class);
manager.exec("ruby", "(java)", 1, 1, "$label.setText(\"This is a test.\")");
Directly calling JRuby APIs

See DirectJRubyEmbedding. The BSF and javax.scripting APIs are strongly recommended, as they are most likely to always do the "right thing", which can change over time in the direct version.

If you plan on calling gems from an embedded script, there are a couple of things you need to be aware of:

Here's an example of a load path from Linux:

irb(main):004:0> puts $LOAD_PATH
/home/username/jruby/lib/ruby/site_ruby/1.8
/home/username/jruby/lib/ruby/site_ruby
/home/username/jruby/lib/ruby/1.8
/home/username/jruby/lib/ruby/1.8/java
lib/ruby/1.8
.
=> nil

Here's an example of a load path for Windows:

C:/common/jruby-0.9.2/lib/ruby/site_ruby/1.8
C:/common/jruby-0.9.2/lib/ruby/site_ruby/1.8/java
C:/common/jruby-0.9.2/lib/ruby/site_ruby
C:/common/jruby-0.9.2/lib/ruby/1.8
C:/common/jruby-0.9.2/lib/ruby/1.8/java
lib/ruby/1.8

These settings are specific to my system. Make sure the paths are correct for your system.

If you declare a bean using BSF, make sure you undeclare it when you are done using it, even if you declare another bean using the same name. BSF internally adds declared beans to a vector, and only removes them once they are undeclared. Or, as an alternative, you can call registerBean and access the object from JRuby using the global $bsh reference.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4