test/markups/README.mediawiki.html (view raw)
1<p>
2<a href="javascript:void(0)">» JRuby Project Wiki Home Page</a>
3
4<h1>Embedding JRuby</h1>
5
6Using Java from Ruby is JRuby's best-known feature---but you can also go in the other direction, and use Ruby from Java. There are several different ways to do this. You can execute entire Ruby scripts, call individual Ruby methods, or even implement a Java interface in Ruby (thus allowing you to treat Ruby objects like Java ones). We refer to all these techniques generically as "embedding." This section will explain how to embed JRuby in your Java project.
7
8</p><p>
9<table id="toc" class="toc" summary="Contents"><tr><td><div style="font-weight:bold">Table of Contents</div><ul></ul></td></tr></table>
10
11</p><p>
12<h1><span class="editsection">[<a href="?section=Red_Bridge_JRuby_Embed">edit</a>]</span> <span class="mw-headline" id="Red_Bridge_JRuby_Embed">Red Bridge (JRuby Embed)</span></h1>
13
14JRuby has long had a private embedding API, which was closely tied to the runtime's internals and therefore changed frequently as JRuby evolved. Since version 1.4, however, we have also provided a more stable public API, known as Red Bridge or JRuby Embed. Existing Java programs written to the <a href="javascript:void(0)">legacy API</a> should still work, but we strongly recommend Red Bridge for all new projects.
15
16</p><p>
17<h2><span class="editsection">[<a href="?section=Features_of_Red_Bridge">edit</a>]</span> <span class="mw-headline" id="Features_of_Red_Bridge">Features of Red Bridge</span></h2>Red Bridge consists of two layers: Embed Core on the bottom, and implementations of <a href="http://www.jcp.org/en/jsr/detail?id=223">JSR223</a> and <a href="http://jakarta.apache.org/bsf/">BSF</a> on top. Embed Core is JRuby-specific, and can take advantage of much of JRuby's power. JSR223 and BSF are more general interfaces that provide a common ground across scripting languages.
18
19</p><p>
20Which API should you use? For projects where Ruby is the only scripting language involved, we recommend Embed Core for the following reasons:
21
22</p><p>
23<ol><li> With Embed Core, you can create several Ruby environments in one JVM, and configure them individually (via <code>org.jruby.RubyInstanceConfig</code>. With the other APIs, configuration options can only be set globally, via the <code>System</code> properties.
24
25</li><li> Embed Core offers several shortcuts, such as loading scripts from a <code>java.io.InputStream</code>, or returning Java-friendly objects from Ruby code. These allow you to skip a lot of boilerplate.
26
27</p><p>
28</li></ol> For projects requiring multiple scripting languages, JSR223 is a good fit. Though the API is language-independent, JRuby's implementation of it allows you to set some Ruby-specific options. In particular, you can control the threading model of the scripting engine, the lifetime of local variables, compilation mode, and how line numbers are displayed.
29
30</p><p>
31The full <a href="http://jruby-embed.kenai.com/docs/">API documentation</a> has all the gory details. It's worth talking about a couple of the finer points here.
32
33</p><p>
34<h1><span class="editsection">[<a href="?section=Previous_Embedding_JRuby_Page">edit</a>]</span> <span class="mw-headline" id="Previous_Embedding_JRuby_Page">Previous Embedding JRuby Page</span></h1>We recommend using Embed Core; however, if you're maintaining code that uses the old API, you can find its documentation on the <a href="javascript:void(0)">legacy embedding</a> page.
35</p>