Published as a follow up to my talk given at RubyManor in November 2008.
A JRuby Twitter client built using MonkeyBars. As demo’d at RubyManor!
Before you start
- JRuby (latest version – currently 1.1.5) (including copy of jruby-complete.jar)
- NetBeans (full version)
- Monkeybars gem (sudo gem install monkeybars)
- Rawr gem (sudo gem install rawr -v 0.3.2)
- Twitter gem (jruby -S gem install twitter)
Create the App skeleton
$. monkeybars projectname
$. cd projectname
$. rawr install
# (choose option 2)
$. jruby -S rake generate ALL=‘src/login’
Install Gems & Unpack Gems
$. jruby -S gem install twitter
$. cd lib/ruby
$. jruby -S gem unpack twitter
$. jruby -S gem unpack hpricot
- File > New Project. Choose Java category > Java Project with existing sources
- Give your app a name and set project folder to where you created your app [hit next]
- Exsisting sources – choose the src directory in your app [hit finish]
Your project will now appear in NetBeans. There are a couple more things to do before you can begin.
- In a terminal (not NetBeans) copy jruby-complete.jar into the lib/java directory.
- Back in NetBeans.. right click the Libraries folder. Choose ‘Add Jar/Folder’ and choose jruby-complete.jar you just added.
- Again right click on the Libraries folder and choose ‘Add Library…’ and select ‘Swing Layout Extensions’
Next we setup a frame to display the GUI for the login:
- Right click on login directory. Choose ‘New > New Frame Form’.
- Name it ‘LoginFrame’ (it can be anything you like but probably best sticking with NameFrame convention)
- You can now start building your interface. woot!
Remember to update your login_view.rb with the name of the frame. e.g.
class LoginView < ApplicationView
Running the project
Still in NetBeans.. Hit the run (play icon) button. It will ask which class you want to choose for the ‘main class’. Choose ‘org.rubyforge.rawr.Main’.
Note, if things don’t go to plan you can still set this by right clicking on the project name and choosing from the menu ‘Set Configuration > Customize…’. The main class can be set in the ‘Run’ section of the configuration dialog that pops up.
Packaging for distribution
rake rawr:bundle:app # Bundles the jar from rawr:jar into a native Mac OS X application (.app)
rake rawr:bundle:exe # Bundles the jar from rawr:jar into a native Windows application (.exe)
rake rawr:bundle:web # Bundles the jar from rawr:jar into a Java Web Start application (.jnlp)
- Embedding images in Netbeans kills the rawr build (null.pointer.exception) – there is a workaround for this in rawr 1.0
- Some Ruby gems won’t run on JRuby e.g. Twitter4R
- Hpricot doesn’t compile using the latest version of RAWR
- Monkeybars doesn’t seem to require frozen gems automatically from lib/ruby
- Make sure you have jruby-complete.jar and swing-layout-1.0.3.jar in you lib/java directory
- Both those jars need to be added to the manifest for packaging.
- Sometimes build paths get set incorrectly, and it can be difficult to find out why..
- Have to remove all mention of “require ‘rubygems’” from frozen gems to make packaging work?!
Sample Application – Download and Source code
- Download for Mac (app)
- Download for Windows (.exe)
- Download for Linux and all other platforms (jar)
git clone git://github.com/martinbtt/tweetobix.git
Or go direct to the Tweetobix project page on github
- http://monkeybars.rubyforge.org/tutorials/usage2.html – Hello world app
At the time of writing there are a few aspects of the packaging process that are not working. (See gotchas…) Hopefully with a bit of further investigation and feedback we can get it working. I will keep updating this page as things progress.
Tweetobix friends timeline page and tweet box.