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)}"
end
def sum(a,b)
# <---------- Something will happen here
end
end
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 thesum
function it has, get the value, and then print it. - The ruby file has been executed successfully.
- I want it to execute the Ruby file
- 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).
WHY AM I ASKING THIS?
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.
-
1You 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– david.pfx2014年03月29日 06:35:49 +00:00Commented 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– Saturn2012年12月20日 19:12:41 +00:00Commented Dec 20, 2012 at 19:12
-
JRuby will run your plain Ruby code on the JVM - your developers can still use Ruby :-).Martijn Verburg– Martijn Verburg2012年12月22日 09:37:58 +00:00Commented Dec 22, 2012 at 9:37
-
-
would you mind explaining more on what it does and what it's good for? "Link-only answers" are not quite welcome at Stack Exchangegnat– gnat2013年03月31日 02:41:35 +00:00Commented Mar 31, 2013 at 2:41
-
1Right you are - slightly expanded answer - HTH!Martijn Verburg– Martijn Verburg2013年04月01日 09:42:59 +00:00Commented 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
System.out.println(result);
}
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.
-
1It is just a JAR file? Where can I find it?Saturn– Saturn2013年01月05日 07:41:44 +00:00Commented Jan 5, 2013 at 7:41
-
1Download 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– akuhn2013年01月05日 08:06:05 +00:00Commented Jan 5, 2013 at 8:06