<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Depth-First: Scripting Java Libraries with Ruby Java Bridge</title>
    <link>http://depth-first.com/articles/2006/08/26/scripting-java-libraries-with-ruby-java-bridge</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>Scripting Java Libraries with Ruby Java Bridge</title>
      <description>&lt;p&gt;&lt;img src="http://depth-first.com/files/ruby.gif" align="right"&gt;&lt;/img&gt;Although &lt;a href="http://jruby.codehaus.org/"&gt;JRuby&lt;/a&gt; solves many Java/Ruby integration issues, in some cases it's not the right solution. One situation is when you want your Ruby code to use extensions written in C. The JRuby documentation makes very clear that this will never be supported. Another situation is if your code needs full access to &lt;a href="http://jruby.codehaus.org/"&gt;Ruby on Rails&lt;/a&gt;, or if your hosting service makes it difficult to configure JRuby on Rails. In these cases, JRuby's currently limited Rails support makes it a suboptimal choice.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://rjb.rubyforge.org/"&gt;Ruby Java Bridge&lt;/a&gt; (RJB) is designed to solve these problems by letting Ruby developers manipulate Java libraries from Ruby. This gives you the ability to access C Ruby extensions &lt;em&gt;and&lt;/em&gt; Java libraries in the same Ruby program. It also makes Rails integration a snap. Articles to follow will explore these two points. For now, let's see how how to get RJB working.&lt;/p&gt;

&lt;p&gt;Installing Ruby Java Bridge is very simple. With root access:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
gem install rjb
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This installs the Ruby Java Bridge gem. That's all there is to it.&lt;/p&gt;

&lt;p&gt;Instantiating and using Java classes consists of the familiar process of first importing the class followed by creating a new instance:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require_gem&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rjb&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rjb&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="ident"&gt;string_class&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Rjb&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="ident"&gt;import&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;java.lang.String&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;hello_string&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;string_class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new_with_sig&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;Ljava.lang.String;&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;hello&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;

&lt;span class="ident"&gt;p&lt;/span&gt; &lt;span class="ident"&gt;hello_string&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;toString&lt;/span&gt; &lt;span class="comment"&gt;# -&amp;gt; &amp;quot;hello&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because an argument is passed to the constructor of the Java class, a special form needs to be used, &lt;tt&gt;new_with_sig&lt;/tt&gt;. The "L" in front of the import statement indicates that the argument "hello" is a non-primitive datatype (i.e. class or interface).&lt;/p&gt;

&lt;p&gt;Ruby Java Bridge offers some important advantages over JRuby. Subsequent articles will explore how these advantages can be used to quickly develop applications integrating chemical informatics libraries written in multiple languages.&lt;/p&gt;</description>
      <pubDate>Sat, 26 Aug 2006 05:46:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9d401e5f-1eb2-4d07-aa76-f577d13e1bf5</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2006/08/26/scripting-java-libraries-with-ruby-java-bridge</link>
      <category>Tools</category>
      <category>ruby</category>
      <category>java</category>
      <category>integration</category>
      <category>rubyjavabridge</category>
    </item>
  </channel>
</rss>
