pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/wiki/JRubyAndJavaCodeExamples

onymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-0fcd9af82350aeda.css" /> JRubyAndJavaCodeExamples · jruby/jruby Wiki · GitHub
Skip to content

JRubyAndJavaCodeExamples

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 = TreeSet.new
set.add "foo"
set.add "Bar"
set.add "baz"
set.each { |v| puts "value: #{v}" }

cm = CallMe.new
cm.hello
$globalCM.hello

class CallJava
  include ISpeaker
  def initialize
    super
    @count = 0
  end

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

Java: ISpeaker.java

package com.example;

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

Java: CallMe.java

package com.example;

public class CallMe {

    String mName;

    public CallMe() {
        this("Default");
    }
    
    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

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy