JRuby on Rails, NetBeans 6 and GlassFish V2 - Simplified Steps

The NetBeans IDE has simplified the steps to deploy JRuby on Rails application on GlassFish. This blog explains the steps and is an update to screencast #web6.

  1. Download the install the latest NetBeans 6 Nightly. I downloaded the Ruby pack by clicking on the "Download" button in the Ruby column.
  2. Create a new Rails Application
    1. Right-click in the Project window and select "New Project...". Take all the defaults as shown below:

    2. Click on "Next" and enter the values as shown below:

      Notice, we have selected "Add rake targets to support app server deployment (.war)". This downloads the goldfish (nee rails integration) plugin and installs in your application. Choose the database that you'd like to work with using the combo box:

      and then click on "Finish" button. I'm choosing the default database (MySQL) in this case.
  3. Create Database
    1. Download and install MySQL Community Server 5.0 (lets say MYSQL_HOME).
    2. Start MySQL database server by giving the command 'mysqld -nt --user root' in MYSQL_HOME/bin directory on Windows or './bin/mysqld_safe' from MYSQL_HOME directory on Unix flavors.
    3. Create a database by giving the following commands:

      mysqladmin -u root create RailsApplication9_development
  4. Create Generators
    1. Generate a model - Right-select the project, select "Generate..." and enter the values as shown below:

      and click on "OK".
    2. Generate a controller - Right-select the project, select "Generate..." and enter the values as shown:

      and click on "OK".
  5. Configure Model and Controller
    1. Configure Model
      1. In the NetBeans IDE, expand "Database Configurations", "migrate" and open "001_create_greetings.rb". Change the "self.up" helper method such that it looks like:

        def self.up
          create_table :greetings do |t|
            t.column :data, :string
      2. Right-select the project, select 'Run Rake Target', 'db', 'migrate'. This generates the appropriate database tables and the following is shown in the output window:

        (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
        == CreateGreetings: migrating =================================================
        -- create_table(:greetings)
        -> 0.1010s
        == CreateGreetings: migrated (0.1110s) ========================================
      3. Add data to the database tables using the following commands in a shell window:

        C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin>mysql --user root
        Welcome to the MySQL monitor. Commands end with ; or \\g.
        Your MySQL connection id is 5
        Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

        Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.

        mysql> use RailsApplication9_development;
        Database changed
        mysql> insert into greetings values (1, "Hello from database!");
        Query OK, 1 row affected (0.10 sec)

        mysql> exit;

      4. In the NetBeans IDE, expand "Configuration" and open "database.yml". Change the database entry in production environment to point to "RailsApplication9_development" instead of "RailsApplication9_production".
    2. Configure Controller
      1. Expand "Controllers", open "say_controller.rb" and change "hello" helper method such that it looks like:

        def hello
          @hello_string = Greeting.find(1).data;
      2. Expand "Views", "say", open "hello.rhtml" and add the following fragment at the bottom of the page:

        <%= @hello_string %>
  6. Create a WAR file
    1. In the NetBeans IDE, right-select the project, select 'Run Rake Target', 'war', 'standalone', 'create' as shown here:

      and the following is shown in the output window:

      (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
      Assembling web application
        Adding Java library commons-pool-1.3
        Adding Java library activation-1.1
        Adding Java library jruby-complete-1.0
        Adding Java library bcprov-jdk14-124
        Adding Java library rails-integration-1.1.1
        Adding web application
        Adding Ruby gem rails version 1.2.3
        Adding Ruby gem rake version 0.7.3
        Adding Ruby gem activesupport version 1.4.2
        Adding Ruby gem activerecord version 1.15.3
        Adding Ruby gem actionpack version 1.13.3
        Adding Ruby gem actionmailer version 1.3.3
        Adding Ruby gem actionwebservice version 1.2.3
        Adding Ruby gem ActiveRecord-JDBC version 0.5
      Creating web archive
    2. This creates "RailsApplication9.war" in the project directory.
  7. Download, Install and Start GlassFish.
    1. Download GlassFish recent build.
    2. Install by giving the command:

      java -jar filename.jar

      This creates "glassfish" directory in your current directory.
    3. In GlassFish install directory, set up the server by giving the following command:

      lib\\ant\\bin\\ant -f setup.xml
    4. Start the server by giving the following command

      bin\\asadmin start-domain
  8. Copy the WAR file (RailsApplication9.war) in "domains/domain/autodeploy" directory.

The application is accessible at "http://localhost:8080/RailsApplication9/say/hello".

Here are the improvements from the last time:

  1. During the project creation, there was an option to select the database to be used by the application. This creates the "database.yml" using default values for that database.
  2. During the project creation, there was an option to add add rake targets for the WAR creation. This made the steps to explicitly install the repository and install the plugin redundant.
  3. ActiveRecord-JDBC 0.5 provides a simplified database configuration and is used by the "war:standalone:create" rake target. This allows the database to be accessed using the Ruby adapter instead of the JDBC adapter and also uses the original format of "database.yml".

Technorati: jruby ruby rubyonrails glassfish netbeans jrubyonglassfish mysql


[Trackback] Earlier in a three-part series (part1, part2, part3) I showed how a JRuby application can be deployed on GlassFish. This screencast consolidates all the entries together and shows how such an application can be developed and deployed using NetBeans...

Posted by Arun Gupta's Blog on September 06, 2007 at 10:40 PM PDT #

when I'm trying to create the war file ('Run Rake Target', 'war', 'standalone', 'create'), Rake is crashing with a timeout.

Posted by JS on September 07, 2007 at 03:21 AM PDT #

Arun, what makes this app a \*JRuby\* on Rails application instead of a \*Ruby\* on Rails application--how do you specify it within the IDE? Also, once you choose, can you switch back and forth between JRuby / Ruby (perhaps to test performance, etc.)? For the latter question, I don't think you can, because the code scripting is different, correct? Thanks, Glen

Posted by Glen on September 07, 2007 at 03:43 AM PDT #

[Trackback] The NetBeans 6 IDE comes pre-configured with JRuby interpreter. This TOTD explains how the JRuby interpreter can be swapped with a C-based Ruby interpreter and vice versa. Verify the JRuby interpreter Create a Rails Hello World using NetBeans 6 IDE....

Posted by Arun Gupta's Blog on September 07, 2007 at 07:43 AM PDT #

I am trying to run through this howto on my Ubuntu system, but the migrate step (5.1.2) bombs. Here is the trace:
\*\* Invoke db:migrate (first_time)
\*\* Invoke environment (first_time)
\*\* Execute environment
\*\* Execute db:migrate
rake aborted!
can't convert nil into String
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:111:in `real_connect'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:389:in `connect'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:152:in `initialize'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `new'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `mysql_connection'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `send'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `connection='
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/query_cache.rb:54:in `connection='
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:230:in `retrieve_connection'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection'
/home/keith/netbeans-6.0-200709101200/ruby1/jruby-1.0.1/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/migration.rb:284:in `migrate'

Posted by Keith on September 10, 2007 at 02:23 PM PDT #

Keith, It seems like this is an issue with ActiveRecord-JDBC. I filed an issue at: http://jira.codehaus.org/browse/JRUBY-1341.

Posted by Arun Gupta on September 11, 2007 at 04:10 AM PDT #

Ruby is new for me.

Posted by heart5 on September 11, 2007 at 06:32 PM PDT #

The best place to get started with Ruby is http://www.ruby-lang.org/en/documentation/. Or if you want to learn JRuby then you can start at: http://jruby.codehaus.org/.

Posted by Arun Gupta on September 11, 2007 at 11:27 PM PDT #

[Trackback] Using the instructions followed in JRuby Hack Day and taking some help from Nick, I figured out how to use the JDBC connection pools configured in GlassFish using the JNDI names. All the commands given below are relevant for GlassFish...

Posted by Arun Gupta's Blog on September 11, 2007 at 11:51 PM PDT #

[Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Ashish described how to create RoRaWAR (Ruby on Rails as Web ARchive). I decided to try these instructions on GlassFish V2 b46. Here are the steps I...

Posted by Arun Gupta's Blog on September 13, 2007 at 01:55 PM PDT #

[Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Follow up from here. In this post I show how a Rails app can talk to database. Here are the steps I followed: Deploy a RoR application...

Posted by Arun Gupta's Blog on September 13, 2007 at 01:55 PM PDT #

[Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Follow up from here. In this post I'll show how a Ruby-on-Rails (RoR) application, talking to MySQL database, can be deployed as a WAR file on GlassFish...

Posted by Arun Gupta's Blog on September 13, 2007 at 01:56 PM PDT #

[Trackback] Jerome has been working on GlassFish gem for Rails. Read the interesting discussion on dev@glassfish. This blog announces a technology preview of this gem and describes the steps to try it out. Download GlassFish Gem from here. If you already...

Posted by Arun Gupta's Blog on September 13, 2007 at 11:13 PM PDT #

Thanks for the instructions. This mostly worked for me on MacOSX with a couple of modifications.

1) I also received Keith's error when running migrations. Adding "host: localhost" to each of the database.yml connection settings (development, test, and production) worked around that. Never had to set that before however. I'd be curious to know the best way to use the JDBC connector as an alternate.

2) The MacOS build for NetBeans includes a separate installer for Glassfish which installed glassfish at /Applications/NetBeans/glassfish-v2b58g/ so I was able to skip running "ant -f setup.xml". Also, "domains/domain/autodeploy" was "domains/domain1/autodeploy" for my installation.

Posted by victor cosby on September 25, 2007 at 03:05 PM PDT #

Thanks for this. It is very clear. It worked - eventually. The issues (which others might be interested in) for me were two. (i) In database.yml change socket: /var/lib/mysql/mysql.sock to host: localhost (as noted above). (ii) I am behind a firewall and cut off from web based resources. I needed to download and copy the following into JRUBY_HOME/lib - commons-pool-1.3.jar, activation-1.1.jar, jruby-complete-1.0.jar, bcprov-jdk14-124.jar, rails-integration-1.1.1.jar

Posted by Richard Forster on October 29, 2007 at 03:37 PM PDT #

The deployed application does not seem to handle 'redirect_to' instructions properly. For instance you could expand 'hello' to the following

def hello
@hello_string = Greeting.find(1).data;
if request.post?
redirect_to(:action => 'tom')

If there is an appropriate view for 'tom' this will work fine in webrick. If the application is deployed (as described above) to Glassfish
The initial invocation of say/hello is ok
but the attempted redirection does not generate a complete URL:
I have tried fiddling with the routes.rb but with no success. link_to seems to be ok.

Posted by Richard Forster on November 11, 2007 at 10:27 AM PST #

thanks all.

Posted by evden eve nakliyat on November 25, 2007 at 02:00 AM PST #

Nice tutorial Arun. I had been struggling to get my database access to work until reading your blog. The key step for me was 5.4 - I didn't think to update the production database information in database.yml. Thanks, Brian.

Posted by Brian Leonard on November 26, 2007 at 09:25 AM PST #

Cool, glad you found it useful.

Posted by Arun Gupta on November 26, 2007 at 11:00 AM PST #

rake war:standalone:create

(in c:/ruby/app)
info: Assembling web application
info: Packing needed java lib....
info: Packing needed Ruby gems...
info: Packing needed files...
info: Creating web archive
reak aborted!
Error: failed to create archive,error code

i'm trying to deploy my rails application into tomcat, installed goldspike but rake is not working
(using ruby not jruby)

Posted by disha on December 13, 2007 at 06:21 PM PST #

If you are using NetBeans 6, then this is a known issue as described at:


I plan to try this outside NetBeans and report back.

Posted by Arun Gupta on December 13, 2007 at 11:15 PM PST #

Not using any type of editors or IDE's

Posted by disha on December 14, 2007 at 08:19 PM PST #

i'm trying to deploy my rails application into tomcat, installed goldspike but rake is not working
(using ruby not jruby)

Posted by BATTERY on November 27, 2008 at 08:53 AM PST #

I realise this thread is from 2007/2008... but I was just checking for developments relating to GlassFish and Rails to see if it was worth looking into again. Based on this, I really don't think it is...

I think it's such a shame that massive investment in Java the platform (ie. not the language - which lets be honest is C++'s slightly more attractive cousin) hasn't truly been able to become a platform for RoR apps. Whilst Ruby on Rails has absolutely flourished since this article was written, it is being deployed now on linux stacks running either good old apache with mod passenger/mod_rails, under unicorn or on platforms like Heroku or EngineYard. I personally would have preferred to see GlassFish and Rails really kick ass together... but it just hasn't stuck, maybe because hosting Java apps has been such a pain in the ass and expensive vs. just hosting linux/apache.

Sun Microsystems and other companies made such a massive investment in Java as a platform and unfortunately the whole thing between Java the platform and Java the language was massively mixed up. I recall being at Java One a few years ago now and listening to a keynote about "huge improvements" to "Java" (you know - the "thing" java) such the iterators and other syntax things introduced in 5... but meanwhile whilst this superficial stuff was happening people were jumping off the boat and finding new levels of productivity and personal satisfaction with Ruby, realising that the JCP wasn't keeping up with DHH and thinking how 90's that Duke character is.

It's such a shame that the billions of dollars and huge intellectual efforts spent on Java as a platform, and the time spent by organisations to implement it and so on isn't really carrying forward. Groovy and Grails are cool, but really hasn't got the widespread community support nor awesome Gems etc. that Rails has... and so whilst I've looked at using it, frankly if I were going to use something other than Ruby/Rails it would be Python, just because since discovering Ruby and Rails my brain now likes Python too.

I do a lot of outsourcing of Rails development projects and I can say that the vast majority (ie. 80%+) of Indian Rails developers do not know what they are doing in Rails... they treat it a bit like PHP, and use NetBeans in a bad way... I think this coupled with the fact that there are so many skilled Indian Java developers (who haven't yet discovered the RoR way of working) presents a tremendous opportunity for Java....if Oracle wants startups and other organisations to continue using Java the platform then I think there should be some strategy (maybe in conjunction with http://www.springsource.com/) around aggressively driving Groovy and Grails usage through NetBeans for the Indian market, and a "cloud offering" that lets developers run their apps cheaply/quickly a bit like heroku would be awesome... otherwise I really think the days of Java for modern web apps are numbered, unless you're in a bureaucratic organisation like a bank.

I can also say that good Rails developers are getting very expensive... if it was less cost to hire good Grails developers and there was a similar level of excitement/energy in the Groovy/Grails world with some Heroku-like deployment options, I'd probably switch to using Groovy/Grails for new projects.

I also think Oracle's strategy and intentions around GlassFish are so unclear, also there is never any clear positioning to say why someone would use it over say Tomcat. So guess what, people just use Tomcat. Given the BEA acquisition and Sun acquisition and that Oracle now own GlassFish and WebLogic... the whole Java app server space makes a lot less sense to me than it ever has...

It is a real shame because I also personally invested a lot of time learning Java, going to conferences etc. (okay I was working for Sun at the time) and now I feel like it's a heavy and largely irrelevant confusing mess of a language and platform and various things that were tried in the mid-late 2000's to make it work with startups that failed...

I still think Java as a platform has huge opportunity... and NetBeans is much better than it ever has been, but the separation from the language and platform still isn't clear to most people, no real Rails developers are using NetBeans... and I don't think and there just isn't enough excitement around Grails/Groovy compared to RoR for me. :-/

Sorry for being a bit brutal, but I think there are a lot of missed opportunities here...

Posted by guest on September 17, 2011 at 08:30 AM PDT #

Post a Comment:
Comments are closed for this entry.

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.

Java EE 7 Samples

Stay Connected


« November 2015