I do not fully understand what am I asking (lol!), well, in the sense of if it is even possible, that is. If it isn't, sorry.

Suppose I have a Java program. It has a Main and a JavaCalculator class. JavaCalculator has some basic functions like

public int sum(int a,int b) {
   return a + b

Now suppose I have a ruby file. Called MyProgram.rb.

MyProgram.rb may contain anything you could expect from a ruby program. Let us assume it contains the following:

class RubyMain
  def initialize
    print "The sum of 5 with 3 is #{sum(5,3)}"
  def sum(a,b)
    # <---------- Something will happen here

rubyMain = RubyMain.new

Good. Now then, you might already suspect what I want to do:

  • I want to run my Java program
    • I want it to execute the Ruby file MyProgram.rb
    • When the Ruby program executes, it will create an instance of JavaCalculator, execute the sum function it has, get the value, and then print it.
    • The ruby file has been executed successfully.
  • The Java program closes.

Note: The "create an instance of JavaCalculator" is not entirely necessary. I would be satisfied with just running a sum function from, say, the Main class.

My question: is such possible? Can I run a Java program which internally executes a Ruby file which is capable of commanding the Java program to do certain things and get results? In the above example, the Ruby file asks the Java program to do a sum for it and give the result.

This may sound ridiculous. I am new in this kind of thing (if it is possible, that is).


I have a Java program, which is some kind of game engine. However, my target audience is a bunch of Ruby coders. I don't want to have them learn Java at all. So I figured that perhaps the Java program could simply offer the functionality (capacity to create windows, display sprites, play sounds...) and then, my audience can simply code with Ruby the logic, which basically justs asks my Java engine to do things like displaying sprites or playing sounds.

That's when I though about asking this.

  • 1
    You have accepted an answer based on JRuby, which is a good solution to a very specific problem. There are many other more general answers that apply to this and similar situations, but if you want to see them, you will probably now have to ask the question again in a different format.
    – david.pfx
    Mar 29, 2014 at 6:35

2 Answers 2


What you're probably really after is JRuby --> http://jruby.org/

JRuby is a fully featured, compliant Ruby implementation on the Java Virtual Machine. According to some Rubyists, it also happens to be the fastest (assuming you're running on a Java 7 compatible VM). JRuby can call into Java libraries and take return values from those calls, in short you can have the best of both worlds.

  • While this sounds promising, I would appreciate a solution to my current scenario (I have plain Java and plain Ruby), without the usage of a "different tool" like JRuby.
    – Saturn
    Dec 20, 2012 at 19:12
  • JRuby will run your plain Ruby code on the JVM - your developers can still use Ruby :-). Dec 22, 2012 at 9:37
  • JRuby is the way to go!
    – akuhn
    Dec 24, 2012 at 4:45
  • would you mind explaining more on what it does and what it's good for? "Link-only answers" are not quite welcome at Stack Exchange
    – gnat
    Mar 31, 2013 at 2:41
  • 1
    Right you are - slightly expanded answer - HTH! Apr 1, 2013 at 9:42

Since you don’t seem to believe us.

Here's how to run ruby code from Java, using JRuby. JRuby is not “a different tool” it is just the JAR file you need to be able to run Ruby on top of Java. It’s as simple as

package com.example;

import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;

public class Main {

    private ScriptingContainer ruby;

    public static void main(String[] args) {
        new Main().run();

    public void run() {
        ruby = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
            // Assign the Java objects that you want to share
        ruby.put("main", this);
            // Execute a script (can be of any length, and taken from a file)
        Object result = ruby.runScriptlet("main.hello_world");
            // Use the result as if it were a Java object

    public String getHelloWorld() {
        return "Hello, worlds!";


As you can see it is even so smart to translate from Ruby’s hello_world to Java’s getHelloWorld naming convention.

  • 1
    It is just a JAR file? Where can I find it?
    – Saturn
    Jan 5, 2013 at 7:41
  • 1
    Download the "JRuby 1.7.2 Complete.jar" file from jruby.org/download (you'll also find language installations on the same page, which are useful for development, but your final system will require the complete.jar only!)
    – akuhn
    Jan 5, 2013 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.