SwingJRubyPackaging

Swing with JRuby Part II: Packaging JRuby Desktop Application

by Teera Kanokkanjanarat

Introduction

The goal of this tutorial is to show you how you can package JRuby desktop application to a distributable Jar file. For more information on how to use JRuby and Java Swing to build desktop application, please check out part I of this tutorial.

Contents


Tutorial Requirements

Before you proceed, make sure you review the requirements in this section.

Software Needed for This Tutorial

Before you begin, install the following software on your computer:

  • NetBeans IDE 6 with Ruby
  • Any additional Jar files needed to run your Ruby application
  • JRuby 1.0.1 release or later. At the time of this writing JRuby 1.0.2 version is bundled with Netbeans 6, or you can download from project home page
  • jruby-complete-1.0.1.jar (or later version). This Jar file is packaged with JRuby download. You can obtain jruby-complete-<version number>.jar from <JRUBY_HOME>/lib folder.

Notations Used in This Tutorial

  • <NETBEANS_HOME> - The NetBeans IDE installation directory
  • <PROJECT_HOME> - The directory that contains the project you create
  • <JRUBY_HOME> - The directory that contain extracted JRuby jar files

Create Netbeans Java project

Our strategy to package Ruby code in a Jar file is to create Java project that can load up Ruby script. This Ruby script will serve as entry point to our application.

1. Open up Netbeans IDE. Under the menu bar, select File > New project. Select 'Java Application' as project type

Image:swingjruby2-1_SwingJRubyPackaging.png

2. Name this project 'SwingJRuby2'. Let the IDE create Main class, click 'Finish'

Image:swingjruby2-2_SwingJRubyPackaging.png

3. This will create a project with empty Main.java class

Image:swingjruby2-3_SwingJRubyPackaging.png

Create Ruby file

This ruby file will serve as an entry point of your application. For this tutorial, we will create a simple Swing JFrame from Ruby class. For more information on how to create a complete JRuby desktop application, please check out part I of this tutorial.

1. Select 'SwingJRuby2' project we have just created. Right click > New Java Package.

2. Create new Java package name 'swingjruby2.scripts'

Image:swingjruby2-4_SwingJRubyPackaging.png

3. Select 'swingjruby2.scripts' package > Right click > create new Ruby file. Name it 'main'. We will now have main.rb file under scripts package

Image:swingjruby2-5_SwingJRubyPackaging.png

4. Open up main.rb and add the following code

include Java

include_class javax.swing.JFrame
include_class javax.swing.JButton

frame = JFrame.new("Swing with JRuby")
button = javax.swing.JButton.new("Hello World!")

frame.get_content_pane.add(button)

# Display
frame.set_default_close_operation(JFrame::EXIT_ON_CLOSE)
frame.pack
frame.visible = true

Note: At the moment Netbeans IDE does not have built-in support for running Ruby file from Java project. If you are looking to developing full blown Ruby project, you can do so in Netbeans Ruby project. After your Ruby application is redy for deployment, you can simply copy over Ruby files to Java application and write Java code to load Ruby scripts. You will see how to do this in the following section.

Add additional Jar files and class libraries to the project

If your Ruby script require Java libraries or Jar files, you must also link this Java project to them.

1. Right click the project > Properties > Java libraries

2. Navigate and include all Jar files and Java libraries require for the project. Netbeans will take care of putting these Jar files to correct folder when we build the application.

Image:swingjruby2-6_SwingJRubyPackaging.png

Add jruby-complete Jar files to your application

This is a critical step. To load up Ruby script from Java code, we need jruby-complete-<version number>.jar file. This Jar file is bundled with JRuby download under <JRUBY_HOME>/lib directory. This tutorial will use jruby-complete-1.0.1.jar. To include jruby-complete jar file to the project, we can follow the same step as including any other Jar files.

1. Right click the project > Properties > Java libraries

2. Navigate and include jruby-complete-1.0.1.jar to the project

Image:swingjruby2-7_SwingJRubyPackaging.png

Edit Main.java to load Ruby script to start application

Now we are ready to modify Main.java to load up Ruby script

1. Open up Main.java

2. Add the following Java code to load up Ruby script


Package swingjruby2;

/**
 * @author teera
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        org.jruby.Main.main(new String[]{"-e require 'scripts/main.rb'"});
    }

}


3. And that's it! With the magic of JRuby, we are now ready to package up this application

Build the project and check output Jar file

To build this project, right click on the project and select build option.

Image:swingjruby2-8_SwingJRubyPackaging.png

Depending on how you configure the location of project output, the output Jar file will be created. By default, the location is <PROJECT_HOME>/SwingJRuby2/dist and all related Jar files will be copied to <PROJECT_HOME/SwingJRuby2/dist/lib and ready to be deployed.

Image:swingjruby2-9_SwingJRubyPackaging.png

Summary

This tutorial shows you how to package up your desktop Ruby application from Netbeans IDE. Netbeans has good support for developing Ruby including Ruby syntax coloring and code hint from Java project type.

As I mention in the note above, it would be great if user can run Ruby script file or open up irb console from Java project. If you want richer Ruby development environment, you can create Ruby project for development. When your application is ready to be deployed, you can copy over lib folder (with all Ruby script files) to a new Java project. The trick here is to make sure that the Java code in Main.java that loads up Ruby script have the right path to navigate to Ruby file.

Please send comment and suggestion to my email teera dot tk at gmail dot com

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo