December 22, 2014
Hot Topics:

JRuby: Java and Ruby Together at Last

  • October 26, 2006
  • By Dominic Da Silva
  • Send Email »
  • More Articles »

Ruby is the programming language on the tip of everyone's tongue these days. The power and ease of software development with Ruby has catapulted web frameworks such as Ruby on Rails into the forefront of web application development. Over the last 10 years, Java has grown to become the leading object-oriented software development language and runs on a multitude of platforms. JRuby is a 100% pure Java implementation of the Ruby programming language. This article gives an overview of JRuby and shows how it leverages the Java language to bring Java developers a fast and powerful alternative for creating application solutions.

Ruby: The Scripting Language that is Taking the Computing World by Storm

It is hard to imagine anyone in the programming world these days who has not heard of Ruby. The ever-increasing popularity of the Ruby on Rails web framework is helping make Ruby the language of choice for rapid application development and testing. Ruby is an interpreted scripting language that provides quick and easy object-oriented programming and contains some neat features such as closures, blocks, and mixins. Ruby is also highly portable, running on Unix/Linux, Windows, and MacOS. For those wanting a more thorough introduction to Ruby, you can read W. Jason Gilmore's article on Ruby.

Java: The Object Oriented Language with 10 Years Under Its Belt

Java was created in the early 1990s by James Gosling at Sun Microsystems. Gosling aimed to provide a virtual machine and a C-style notation language with greater uniformity and simplicity than C/C++. Another major goal of Java was to provide a "Write Once Run Anywhere" (WORA) paradigm, allowing a program to be written on one platform and run on another platform without recompilation to the target platform. The Java platform consists of a Java Runtime Environment and Java Software Development Kits (SDKs). Java now provides SDKs for platforms ranging from mobile devices to enterprise systems with its Java SE, EE, and ME technology stacks. Over the last 10 years, Java has been used on mobile devices, kiosks, web applications, financial systems, and real-time systems.

JRuby: Ruby on the Java Virtual Machine (JVM)

JRuby is a 100% pure implementation of the Ruby language, released under a tri-license of CPL, GPL, and LGPL. It is a 1.8.4 Ruby interpreter, with most of the Ruby built-in classes provided. JRuby supports interacting with and defining Java classes from within a Ruby program as well as Bean Scripting Framework support. The current version as of this writing is JRuby 0.9.1. JRuby provides Ruby programs with access to Java classes, allowing them to be used as first-class objects within them. JRuby's creators, Thomas Enebo and Charles Nutter, have been hired by Sun to work on JRuby full time.

Running Your First JRuby Program

The JRuby distribution comes as a tar.gz file. For this article, I worked on a Windows-based system using the jruby-bin-0.9.1.tar.gz binary distribution of JRuby and Java SE version 1.5.0_06. After uncompressing the distribution, I ended up with a jruby-0.9.1 folder that contained subfolders: bin, docs, lib, and samples. You can add a JRUBY_HOME environment variable pointing to this directory and then add the %JRUBY_HOME%bin ($JRUBY_HOMEbin on UNIX) to your system PATH. The JRuby distribution's bin directory contains the jruby.bat file that is used to run the JRuby interpreter. Run the command jruby –version from the command line to test that the JRuby is working:

C:JRubyjruby-0.9.1>jruby -version
ruby 1.8.5 (0) [java]

In the samples directory of the distribution, you will find some sample Ruby programs that exhibit the functionality of JRuby. As a first example, look at the java2.rb sample. This sample contains the following Ruby code:

require "java"

filename = "./samples/java2.rb"
fr = java.io.FileReader.new filename
br = java.io.BufferedReader.new fr

s = br.readLine
print "------ ", filename, "------n"

while s
   puts s.to_s
   s = br.readLine
end

print "------ ", filename, " end ------n";
br.close

The first line enables JRuby's Java support and allows a Ruby program to use Java classes.

The program defines a java.io.FileReader pointing to the file "./samples/java2.rb". It then declares a java.io.BufferedReader that buffers the data read via the FileReader. The program reads all the lines from the file and prints them to the console. Here is the result of running this Ruby script with JRuby:

C:JRubyjruby-0.9.1>jruby .samplesjava2.rb
------ ./samples/java2.rb------
require "java"

filename = "./samples/java2.rb"
fr = java.io.FileReader.new filename
br = java.io.BufferedReader.new fr

s = br.readLine

print "------ ", filename, "------n"

while s
   puts s.to_s
   s = br.readLine
end

print "------ ", filename, " end ------n";

br.close
------ ./samples/java2.rb end ------

C:JRubyjruby-0.9.1>

So, you have used JRuby to run a Ruby script that reads a file using Java classes and prints it out. This is a powerful concept that JRuby now brings to Ruby users.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel