Thursday, October 16, 2008

Using Ruby Java Bindings on Dreamhost

Getting rjb, also known as "Ruby Java Bindings' to work in a Dreamhost account can be somewhat problematic. Fortunately, it is also fairly straightforward. You just have to install all the dependencies in the user directory.

In my case, I was setting up a Rails Application that used the iText Java library to fill in PDF documents for user download. Of course, the server environment was not using Sun Java and the Java headers were not present, so gem install rjb failed. Joy.

The first course of action was to do a local install of Java.

Download jdk-6u7-linux-x64.bin and jre-6u7-linux-x64.bin from the Sun Java site. Then create an ~/opt directory and extract the JRE and JDK (you will need to chmod u+x both files then call them from the command line). Move the resulting folders to ~/opt . I renamed the folders to jdk and jre for simplicity.

Now you ensure that user gems are enabled in cPanel. Then add the following 3 lines to ~/.bash_profile

export JAVA_HOME=/home/username/opt/jdk
export GEM_PATH=/home/username/ruby/gems
export GEM_HOME=/home/username/ruby/gems

Run source ~/.bash_profile to load the paths.

Now you can run gem install rjb without any problems. You will likely have to re-install Rails and other gems because we will be telling our Rails app to load gems from the user directory. Just use the regular gem install syntax.

Add the following 2 lines to your config/environment.rb at the top

ENV['GEM_PATH']='/home/username/ruby/gems'
ENV['JAVA_HOME']='/home/username/opt/jdk'

That's pretty much it.

I will concede that these probably aren't the best instructions, but this is the real meat of the solution. If you keep getting "no such file to load" errors, you will need to extract the gems to the vendor/plugins directory. cd RAILS_ROOT/vendor/plugins and gem unpack gem_name for each problematic gem. I believe this is an issue with Passenger.

Please correct me if I am wrong about any of this, it was a very long day!

2 comments:

Thimmaiah said...

Patrick - Im trying to do the same stuff - use rjb to access some java code. However DH keeps killing my process saying its crossing memory limits. Did you run into this prob ?

Patrick said...

If I remember correctly, you should place the following in your environment.rb to specify the memory that the JVM is permitted to use. Increase -Xmx32m as necessary for your application to function.

ENV['_JAVA_OPTIONS']='-Xms16m -Xmx32m'