# Best way to invoke JRuby from Java

**URL:** <https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686>\
**Category:** JRuby\
**Created:** [May 26, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686 "2008-05-26T18:16:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 26, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/1 "2008-05-26T18:16:14Z")

</div>

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](http://wiki.jruby.org/wiki/Java_Integration)  
and ([http://wiki.jruby.org/wiki/Direct\_JRuby\_Embedding](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

```

---

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 26, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/2 "2008-05-26T22:21:33Z")

</div>

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](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  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://xircles.codehaus.org/manage_email)

–  
Blog: [http://www.bloglines.com/blog/ThomasEEnebo](http://www.bloglines.com/blog/ThomasEEnebo)  
Email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) , [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 27, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/3 "2008-05-27T14:33:59Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 27, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/4 "2008-05-27T14:58:06Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 28, 2008, 5:47am UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/5 "2008-05-28T05:47:27Z")

</div>

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](http://xircles.codehaus.org/manage_email)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Matt\_Fletcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c57346/32.png) [@Matt\_Fletcher](https://www.ruby-forum.com/u/Matt_Fletcher)\
**Post date:** [May 28, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/best-way-to-invoke-jruby-from-java/138686/6 "2008-05-28T15:59:04Z")

</div>

On Tue, May 27, 2008 at 7:55 AM, Matt Fletcher  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.bloglines.com/blog/ThomasEEnebo)  
Email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) , [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```
