Skip to content


Charles Oliver Nutter edited this page Feb 16, 2017 · 2 revisions

JRuby and Java Code Examples

Below are some code examples showing how to call JRuby from Java and how to call Java from JRuby.

Table of Contents

JRuby calling Java

See also: Calling Java from JRuby

JRuby: call_java.rb

require "java"

java_import "java.util.TreeSet"
java_import "com.example.CallMe"
java_import "com.example.ISpeaker"

puts "Hello from ruby"
set =
set.add "foo"
set.add "Bar"
set.add "baz"
set.each { |v| puts "value: #{v}" }

cm =

class CallJava
  include ISpeaker
  def initialize
    @count = 0

  def say(msg)
    puts "Ruby saying #{msg}"
  def addOne(from)
#    m.synchronize {
      @count += 1
      puts "Now got #@count from #{from}"
#    }


package com.example;

public interface ISpeaker {
    public void say(String msg);
    public void addOne(String from);


package com.example;

public class CallMe {

    String mName;

    public CallMe() {
    public CallMe(String name) {
        mName = name;
    public void hello() {
        System.out.println("Hello from "+mName);
    public static void main(String []args) {
        System.out.println("Called main");

Java calling JRuby

See the following pages for updated Java embedding documentation:

RedBridge - The RedBridge API is the recommended Java API to use when embedding JRuby into a Java application or writing Java code to drive JRuby.

Embedding with JSR-223 - JSR-223, also known as the the `javax.script` API, is a standard Java API for driving embedded JVM languages. It is not as direct as RedBridge but it is standard across all JVM languages that support it.

Clone this wiki locally