Hi.
Short version:
I need to start JRuby from Java. I’m wondering what the preferred way
is to do that now. I’ve perused two wiki pages
(http://wiki.jruby.org/wiki/Java_Integration
and (http://wiki.jruby.org/wiki/Direct_JRuby_Embedding) and they are
chock full of ideas. But there are so many suggestions I’m not clear
on what the preferred way to invoke JRuby from Java 5 is. Not to
mention those wiki pages may be stale.
Long version:
For almost a year now on two different projects, my company has been
rolling our JRuby desktop applications into a single jar file. We use
jarjar to combine jruby-complete, our Ruby files, and some other Java
libraries all into one. Here is what the jar’s Main-Class looks like:
============
public class Main {
public static void main(String[] args) {
String[] jrubyArgs = new String[2 + args.length];
jrubyArgs[0] = “-e”;
jrubyArgs[1] = “require ‘application_bootstrap’”;
for (int i = 2; i < 2 + args.length ; i++) {
jrubyArgs[i] = args[i - 2];
}
org.jruby.Main.main(jrubyArgs);
}
}
Where ‘application_bootstrap’ is the name of our Ruby program’s
bootstrap class. As you can see from this, we’re passing a list of
JRuby arguments to org.jruby.Main.main. This is how the JRuby start
script (bin/jruby in $JRUBY_HOME) works, but that isn’t necessarily
the public interface. The technique I posted above has an continues to
work great for us, but I’d really like to use whatever is considered
JRuby’s public interface.
PS
We’ve also found this to work:
import org.jruby.Ruby;
import org.jruby.javasupport.JavaEmbedUtils;
public class Main {
public static void main(String[] args) {
Ruby runtime = Ruby.newInstance();
runtime.evalScriptlet(“require ‘application_bootstrap’”);
IApplicationBootstrap application =
(IApplicationBootstrap)JavaEmbedUtils.rubyToJava(runtime,
runtime.evalScriptlet(“ApplicationBootstrap.new”),
IApplicationBootstrap.class);
application.run();
}
public interface IApplicationBootstrap {
public void run();
}
}
Where the Ruby ApplicationBootstrap class implements the
IApplicationBootstrap interface. Is this preferable over using
org.jruby.Main?
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
The interfaces in JavaEmbedUtils are meant to be long-lasting API’s
for “raw” embedding. This snippet from
(http://wiki.jruby.org/wiki/Direct_JRuby_Embedding) is the best way to
initialize and eval things:
import org.jruby.Ruby;
import org.jruby.RubyRuntimeAdapter;
import org.jruby.javasupport.JavaEmbedUtils;
{…}
// Create runtime instance
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
{…}
evaler.eval(runtime, “puts 1+2”);
{…}
// Shutdown and terminate instance
JavaEmbedUtils.terminate(runtime);
-Tom
On Mon, May 26, 2008 at 11:14 AM, Matt Fletcher
[email protected] wrote:
jrubyArgs[0] = "-e";
class. As you can see from this, we’re passing a list of JRuby arguments to
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
–
Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: [email protected] , [email protected]
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Ok. I went ahead and added the first sentence of your post to the wiki
page that you linked.
On May 26, 2008, at 4:06 PM, Thomas E Enebo wrote:
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
-Tom
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Another question:
The method I posted below that uses org.jruby.Main.main allows me to
pass JRuby arguments, like -r, -e, etc. as if they were from the
command line. Is there any way to do the equivalent with the technique
you posted? I took a look at JavaEmbedUtils and RubyRuntimeAdapter but
nothing stood out to me. Maybe I need to do something with that Ruby
runtime?
On May 26, 2008, at 4:06 PM, Thomas E Enebo wrote:
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
-Tom
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Well the short answer is “not easily”. But if you look at
RubyInstanceConfig, accessible from runtime.getInstanceConfig, I think
you might get some ideas. That’s where command-line processing happens,
so it’s possible we could expose that in a nicer JavaEmbedUtils API.
Matt Fletcher wrote:
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
-Tom
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
On Tue, May 27, 2008 at 7:55 AM, Matt Fletcher
[email protected] wrote:
Another question:
The method I posted below that uses org.jruby.Main.main allows me to pass
JRuby arguments, like -r, -e, etc. as if they were from the command line. Is
there any way to do the equivalent with the technique you posted? I took a
look at JavaEmbedUtils and RubyRuntimeAdapter but nothing stood out to me.
Maybe I need to do something with that Ruby runtime?
As Charles noted you can getInstanceConfig. We could also probably
accept a string in later versions of JavaEmbedUtils initialize.
Largely, it is understanding the requirements of embedders that has
made us update our embedding interface. This sounds like a useful
requirement. Could you file this as an enhancement Jira issue on our
site please?
-Tom
–
Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: [email protected] , [email protected]
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email