Getting Started With JRuby on Rails for Sun GlassFish Enterprise Server v3 Prelude

Accessing Java Libraries from a Rails Application

The primary advantage of developing with JRuby is that you have access to Java libraries from a Rails application. For example, say you might want to create an image database and a web application that allows processing of the images. You can use Rails to set up the database-backed web application and use the powerful Java 2DTM API for processing the images on the server-side.

This section shows you how to get started using Java libraries in a Rails application while stepping you through building a simple Rails application that does basic image processing with the Java 2D API.

This application demonstrates the following concepts involved in using Java libraries in a Rails application:

For simplicity's sake, this application does not use a database. You will need a JPEG file to run this application.

ProcedureCreating the Rails Application That Accesses Java Libraries

  1. Go to <JRUBY_HOME>/samples.

  2. Create an application by running this command:

    jruby -S rails imageprocess
  3. Open the <JRUBY_HOME>/samples/imageprocess/config/environment.rb file in a text editor.

  4. Follow steps 2 and 3 from the instructions in section, Using Rails Without a Database.

  5. Go to the <JRUBY_HOME>/samples/imageprocess directory you just created.

  6. Create a controller and default view for the application by running this command:

    jruby script/generate controller home index
  7. Go to the <JRUBY_HOME>/samples/imageprocess/app/views/home directory.

  8. Create a second view by copying the default view into a view called seeimage.html.erb:

    cp index.html.erb seeimage.html.erb

ProcedureCreating the Views That Display the Images Generated by Java2D Code.

With this task, you will perform the following actions:

  1. Find a JPEG image that you can use with this application.

  2. Add the image to <JRUBY_HOME>/samples/imageprocess/public/image file.

  3. Go to <JRUBY_HOME>/samples/imageprocess/app/views/home file.

  4. Open the index.html.erb file in a text editor.

  5. Replace the contents of this file with the following HTML markup:

    		<img src="../../images/kids.jpg"/><p>
    		<%= link_to "Perform a ColorConvertOp on this image", :action => "seeimage" %>

    This page loads an image from <JRUBY_HOME>/samples/imageprocess/public/images and provides a link that references the seeimage action. The seeimage action maps to the seeimage view, which shows the processed image.

  6. Replace kids.jpg from line 3 of index.html.erb with the name of your image that you saved from step 3 of this procedure.

  7. Save index.html.erb file.

  8. Open seeimage.html.erb file in a text editor.

  9. Replace the contents of this file with the following HTML markup:

    		<img src="/home/processimage"/><p>
    		<%= link_to "Back", :action => "index" %>

    The img tag on this page accesses the processimage action in HomeController. The processimage action is where you will put the Java2D code to process the image you loaded into index.html.erb.

ProcedureAdding Java2D Code to a Rails Controller

With this task, you will add the code to process your JPEG image.

  1. Add the following line to HomeController, right after the class declaration:

    include Java

    This line is necessary for you to access any Java libraries from your controller.

  2. Create a constant for the BufferedImage class so that you can refer to it by the shorter name:

    BI = java.awt.image.BufferedImage
  3. Add an empty action, called seeimage, at the end of the controller:

    def seeimage

    This action is mapped to the seeimage.html.erb view.

  4. Give controller access to your image file using, making sure to use the name of your image in the path to the image file. Place the following line inside the seeimage action:

    filename = "#{RAILS_ROOT}/public/images/kids.jpg"
    imagefile =

    Notice that you don't need to declare the types of the variables, filename or imagefile. JRuby can tell that filename is a String and imagefile is a instance because that's what you assigned them to be.

  5. Read the file into a BufferedImage object and create a Graphics2D object from it so that you can perform the image processing on it. Add these lines directly after the previous two lines:

    bi =
    w = bi.getWidth()
    h = bi.getHeight()
    bi2 =, h, BI::TYPE_INT_RGB)
    big = bi2.getGraphics()
    big.drawImage(bi, 0, 0, nil)
    bi = bi2
    biFiltered = bi

    Refer to The Java Tutorial for more information on the Java 2D API. The important points are :

    • You can call Java methods in pretty much the same way in JRuby as you do in Java code.

    • You don't have to initialize any variables.

    • You can just create a variable and assign anything to it. You don't need to give it a type.

  6. Add the following code to convert the image to grayscale:

    colorSpace = java.awt.color.ColorSpace.getInstance(
    op =, nil)
    dest = op.filter(biFiltered, nil)
    big.drawImage(dest, 0, 0, nil);
  7. Stream the file to the browser:

    os =
    javax.imageio.ImageIO.write(biFiltered, "jpeg", os)
    string = String.from_java_bytes(os.toByteArray)
    send_data string, :type => "image/jpeg", :disposition => "inline", 
    	:filename => "newkids.jpg"

    Sometimes you need to convert arrays from Ruby to Java code or from Java code to Ruby. In this case, you need to use the from_java_bytes routine to convert the bytes in the output stream to a Ruby string so that you can use it with send_data to stream the image to the browser. JRuby provides some other routines for converting types, such as to_java to convert from a Ruby Array to a Java String. See Conversion of Types.

ProcedureRunning a Rails Application That Uses Java 2D Code

  1. Deploy the application on the GlassFish v3 Gem:

    jruby -S glassfish_rails imageprocess
  2. Run the application by entering the following URL into your browser:


    You should now see an image and a link that says, “Perform a ColorConvertOp on this image.”

  3. Click the link.

    You should now see a grayscale version of the image from the previous page.