I am using the JRuby 1.1.2 release.
I’ve been working on getting JRuby working as an embedded interpreter
in a larger Java system that I’m working with (I’m the ruby guy, not
the Java guy). I’ve successfully adapted the RubyLauncher class
suggested in the wiki [1] for my own use. Testing has progressed to
the point where I can instantiate a ruby class and execute methods
within it. However, this only works if I instantiate the RubyLauncher
helper class with an absolute path that points to my ruby file on the
file system.
I need to package the ruby files up in the jar with everything else.
I’ve tried adding all of the paths within the jar using the #loadPaths
facility within the RubyLauncher helper, but no joy. As soon as I
remove the absolute path from my ‘new RubyLauncher()’ call, it can’t
find the ruby file. I get the following exception (visible in jdb).
Exception occurred: org.jruby.exceptions.RaiseException
(uncaught)“thread=SchedulingUnitThread|tid=1114683744|”,
org.jruby.evaluator.ASTInterpreter.rootNode(), line=1,656 bci=88
Here’s some code:
public class RubyLauncher {
private IRubyObject rootRubyObject;
private Ruby runtime;
/**
*
-
@param initialRequire The name of the .rb file that is your
starting point (on your claspath). -
@param rootRubyClass The name of the ruby class in the above .rb
file, must have no-arg constructor (a new instance will be created).
*/
public RubyLauncher(String initialRequire, String rootRubyClass) {
String bootstrap =
"require \"" + initialRequire + "\"\n"+
"class Bootstrap \n" +
" def initialize \n" +
" @object = " + rootRubyClass + ".new \n" +
" end \n" +
" def execute_no_params(method_name) \n" +
" @object.send(method_name) \n" +
" end \n" +
" def execute_with_param(method_name, param) \n" +
" @object.send(method_name, param) \n" +
" end \n" +
"end \n" +
"Bootstrap.new";
// This list holds the directories where the Ruby scripts can be
found.
List loadPaths = new ArrayList();
loadPaths.add(“com/audentestech/util/lib”); // add “lib” and others
as necessary
loadPaths.add(“com/audentestech/ruby/lib”); // add “lib” and others
as necessary
loadPaths.add(“/lib”); // add “lib” and others as necessary
runtime = JavaEmbedUtils.initialize( loadPaths );
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
rootRubyObject = evaler.eval( runtime, bootstrap );
} // end of constructor method
}
Can any other folks who have successfully embedded JRuby give me a
hand? Thanks!
cr
[1] http://wiki.jruby.org/wiki/Direct_JRuby_Embedding
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email