Can you call compiled JRuby classes from java?

So, I came up with the general idea of ​​writing code in JRuby and then accessing all classes through Java. I have no idea if this is possible, but I still wanted to ask. Let's say I have JRuby code:

class adder def addme return 22 end end 

If I compiled this with jrubyc, I could then do something like this in java:

 import adder; class anything { void testMethod() { adder a = new adder(); int x = a.addme(); } } 

Having looked at it now, I think that Java will have a zero idea of ​​which application for the test element test will be returned so that it does not work. I don’t know, but I still wanted to throw it away.

thanks

+7
java ruby compilation jruby
source share
3 answers

Actually a new way to do this in JRuby 1.5! Your question is very timely. Here is an example session:

http://gist.github.com/390342

We hope the blog post will be described in detail in this new feature. Here are some preliminary JRuby wiki docs:

http://wiki.jruby.org/GeneratingJavaClasses

+8
source share

There are actually two ways to call ruby ​​code from java: the first is slower, but you can change it at runtime to call the script engine, as from this link . but as for how you did it, jrubyc compiles ruby ​​into javaBytecode, which means java will see it as java code

jrubyc adder.rb --java Compile the file "adder.rb" as the class "Adder.class"

and just as you did ...

so you would call it like any other java class

 import org.jruby.RubyObject Adder ad = new Adder(); RubyObject ro = ad.addme(); 

resource

+1
source share

This is possible using the built-in package in JRuby, but I think that is beyond the scope of the answer here. Check this out: http://kenai.com/projects/jruby/pages/RedBridge

0
source share

All Articles