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.
Before you proceed, make sure you review the requirements in this section.
Before you begin, install the following software on your computer:
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
2. Name this project 'SwingJRuby2'. Let the IDE create Main class, click 'Finish'
3. This will create a project with empty Main.java class

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'
3. Select 'swingjruby2.scripts' package > Right click > create new Ruby file. Name it 'main'. We will now have main.rb file under scripts package
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.
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.
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
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
To build this project, right click on the project and select build option.
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.
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
| swingjruby2-1.png | ![]() |
62413 bytes |
| swingjruby2-2.png | ![]() |
50007 bytes |
| swingjruby2-3.png | ![]() |
79737 bytes |
| swingjruby2-4.png | ![]() |
44113 bytes |
| swingjruby2-5.png | ![]() |
43068 bytes |
| swingjruby2-6.png | ![]() |
65943 bytes |
| swingjruby2-7.png | ![]() |
66564 bytes |
| swingjruby2-8.png | ![]() |
35737 bytes |
| swingjruby2-9.png | ![]() |
62767 bytes |