javajrubyjavax.script

How can I instantiate a JRuby class that implements a Java interface in Java


There is a Java interface Job:

interface Job{
...
}

and a JRuby class SimpleJob that implements it:

class SimpleJob
require org.quartz.Job
...
end

I need to (from a Java class) instantiate the SimpleJob class using the javax.script.ScriptEngine class, and get its class object. How do I do this?


Solution

  • "easiest" way is doing an eval :

    IRubyObject instance = (IRubyObject) rubyEngine.eval("SimpleJob.new");
    

    to access the class and "call" it by hand :

    RubyClass klass = (RubyClass) rubyEngine.eval("SimpleJob");
    klass.callMethod("new");
    

    NOTE: it's also possible to achieve the same using JRuby's "direct" API :

    runtime.getClass("SimpleJob").callMethod("new");
    

    assuming you obtained a org.jruby.Ruby runtime instance