Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

JRuby on Rails: The power of Java, the simplicity of Ruby on Rails

Benefit from the combination of the latest and greatest Web framework and the powerful, industry-standard JVM

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

Why run Rails on the JVM?

If your project is now using Java frameworks such as Spring and Hibernate, there is no need to change. But if you have the flexibility to choose a new approach for your next project, consider Rails. Even easier to use than lightweight Java frameworks, Ruby on Rails seems like a natural next step in a Web developer's journey towards simplicity and expressiveness.

Unfortunately, making the transition to a new language is generally considered a dangerous step, and managers are leery of the risk. JRuby can make Rails more acceptable to management. On the JVM, Rails becomes a Java framework. In fact, non-Java languages in "Java" Web frameworks are commonly used, as, for example. in JavaServer Pages. (See Bruce Tate's book Java to Ruby for more strategies for getting management buy-in.)

Just as operating systems, typically written in C/C++, provide the infrastructure for applications written in more abstract languages, so too the Java platform plays the role of "system software" for dynamic languages like Ruby, providing infrastructure-level support. A tremendous variety of functionality is accessible through Java today. APIs such as JDBC and JMS (Java Message Service) are best in class, and many irreplaceable in-house or independent software vendor enterprise information systems are accessible through Java APIs. By using JRuby, Rails applications can call into existing Java libraries as easily as Java code can.

With JRuby, Rails applications can run alongside Java Web applications on existing Java EE application servers. These application servers have a strong technical infrastructure. On the human side, education programs, experienced developers  and support personnel are commonly available. And simply by running on the JVM, these application servers gain the benefit of the immense optimization efforts put into the JVM over the last decade.

The JVM has a much more sophisticated security model than Ruby's, giving JRuby on Rails tools for dealing with Web applications' typical security challenges, including control of Ruby scripts received from various sources. It also includes built-in support for internationalization.

In "Ruby for the Java World," I described the move towards dynamic languages as part of a long-term trend up the abstraction ladder towards higher-level languages. Running the Ruby interpreter in the managed environment of the JVM rather than at the operating system level is, again, a step up to a more abstract layer. So is the use of Rails as a standard implementation of the Web-application layer, leaving only application-specific functionality in developers' hands.

Getting started with JRuby on Rails

The JRuby interpreter is closing in on its goal of near complete compatibility with the original interpreter, known as Matz's Ruby Interpreter (MRI) for its creator, Yukihiro Matsumoto. Though some rough edges need to be smoothed out, as of version 0.9.2, JRuby can now run Rails.

Although you can skip straight to installing Rails with JRuby, I suggest you get it going with MRI as a way of learning the system and validating your installation. The simplest way to install Ruby on Rails is with one of the all-in-one bundles that includes Ruby, Rails, Apache and the MySQL database. (See InstantRails and Locomotive in Resources.)

Install JRuby. Next, install Rails code into JRuby using the gem tool, available in the bin directory under your JRuby home, with the command gem install rails -y --no-ri --no-rdoc. (The last two switches skip the documentation generation, which can be slow.)

The above instructions will get you going with JRuby on Rails, but as you get comfortable with it, you have some other choices. Rails lets you use most common relational databases such as Oracle and SQL Server, Web servers such as Apache, and application servers such as Tomcat. You also have the option of accessing databases using the ActiveRecord-JDBC adapter (see JRuby wiki) rather than Ruby-native drivers.

As you continue working with JRuby on Rails, you may want to temporarily keep MRI on hand, as it is faster and more stable than JRuby (although that gap will close soon). It's useful for running development scripts such as migrations and generators, as well as for validating your Web application if you suspect a bug in JRuby. To do this, you'll need to keep your application free of calls to Java libraries or else let it choose between Java libraries and pure-Ruby calls. You can distinguish the interpreter by checking a built-in constant: if RUBY_PLATFORM =/java/ {…}.

The Java libraries used by the application have to be in JRuby's classpath. To customize the jar files used by each JRuby application, create a batch (or shell) file, which sets the classpath and then calls the basic JRuby batch file. You can also alter the system classpath or edit the basic batch file.

I've provided an example, available for download from Resources, that shows a simple Ruby on Rails Web application that distributes long running tasks among worker processes. This example illustrates the combination of the distributed processing power of a JavaSpace with the speed of development of a Rails application. The code for integrating the two, calling from Rails into a Java API, is as simple as calling from Java to Java. In addition, because tasks are expressed in Ruby, users can develop and submit them with ease. Neither Ruby nor Java alone could have achieved this. (See Resources for downloadable code. Instructions are at doc/README_FOR_APP. The application's Ant file build.xml also illustrates how to set up and run JRuby on Rails applications from Ant.)

Challenges

As the release number suggests, JRuby 0.9.2 is still not ready to run production applications. Some bugs need to be fixed, and for the time being, JRuby is slower than MRI. Using a Java application server with Rails requires a non-standard adapter servlet, and building war files requires a special Ant script, neither of which is yet a standard part of the JRuby distribution.

Rails is particularly weak at dealing with legacy components, a result of the opinionated software philosophy, which provides excellent support for one good way of solving most common problems, but less flexibility for alternative approaches. For example, ActiveRecord assumes that each table has a single primary key column called id. Although you can substitute a key column for another name, you cannot define a multiple-column key without introducing a special plug-in. Java frameworks like Hibernate, in contrast, slow development in the simple (and common) cases, but handle the corner cases and legacy code much better than Rails.

Ultimately, the major challenge to adopting Rails will be the desire to standardize on existing languages and frameworks. Rails shines for new projects and new organizations, where this is less of a barrier.

Lessons from Rails

Even if you can't adopt Rails into your Java shop today, you can learn a lot by studying it and running some sample code. Rails serves as an epitome of the trend towards lightweight technologies and as an outstanding example of principles that every application should follow. Rails demonstrates dynamism and metaprogramming techniques, which can be adopted in Java, albeit with some difficulty. Rails' "batteries-included" integration of common functionalities sets an example for other framework providers. (See Bruce Tate's "Crossing Borders" series.)

As an example, the Trails framework implements many of the principles of Rails in the Java language. Although it cannot achieve the full flexibility of a Ruby-based framework, it uses concepts like reflection, metaprogramming (using annotations), and convention over configuration. It builds on the lightweight Java frameworks Spring, Hibernate and Tapestry to allow all tiers of a high-quality, maintainable Web application to be declared with a POJO class representing the business domain. Though Trails, like Rails, targets Web applications, the lessons can be applied to frameworks in other areas as well.

JRuby on Rails: The perfect combination

Rails is quickly becoming the leader in lightweight Web application development. With JRuby, Rails will gain access to the functionality, power, and industry acceptance of Java libraries and the JVM. Rails makes it a pleasure to develop for the Web and lets you focus on what's specific to your application.

What has your experience been with JRuby and/or Ruby on Rails? Share your thoughts in the discussion thread that appears below.

My thanks to Charles O. Nutter for his valuable comments on this article. Any remaining errors are mine.

About the author

Joshua Fox is a software architect based in Israel. He has worked with Java since 1996. His experience with dynamic languages in the JVM runs from JavaScript-controlled VoIP telephony in the browser to an implementation of a Python-based ontological expression language.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

PROBABLY A GAYBy Anonymous on October 28, 2009, 2:30 pmONLY gays can write things like this, because tehy have never seen teh real world.

Reply | Read entire comment

Why no Opensource OS?By Anonymous on June 8, 2009, 3:57 pmI have to rent a windows OS or an Apple OS in order to run an OpenSource Web development tool? I run Debian and I seem to be hitting walls with getting JRuby on...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • An example, available for download, shows a simple Ruby on Rails Web application that distributes long running tasks among worker processes through JavaSpaces.
  • The blogs of JRuby project leaders Thomas E. Enebo and Charles O. Nutter give running updates on the status of JRuby and JRuby on Rails.
  • "Ruby for the Java World," Joshua Fox (JavaWorld, July 2006) introduces the JRuby interpreter.
  • The Ruby on Rails homepage.
  • From Java to Ruby: Things Every Manager Should Know, by Bruce Tate (The Pragmatic Programmers, June 2006; ISBN: 0976694093) describes how to make the transition to Ruby from the managerial perspective. It's useful for convincing your manager to take the leap. It also briefly discusses JRuby on Rails.
  • InstantRails, the everything-included, no-install bundle of what you need to run Rails on Windows; and Locomotive, the equivalent for Macintosh OS X. Get started with one of these, then switch interpreters from MRI to JRuby.
  • The JRuby homepage.
  • The JRuby wiki gives practical instructions on the rapidly evolving JRuby on Rails at the "Getting Started" page; see also the Wiki page on the ActiveRecord JDBC adapter.
  • Use the new standard scripting API when running JRuby on Java 6. See the homepage for the Scripting project with the instructions and the library for running JRuby with the Java 6 scripting API.
  • Bruce Tate's "Crossing Borders" series explores the advantages of dynamic languages and the insights Java programmers can gain from them.
  • The Trails framework implements many of the principles of Rails in Java.
  • For more articles on developing Web applications, browse through the articles in JavaWorld's Web Development Frameworks Research Center.
  • Keep up with what's new at JavaWorld! Sign up for our free Enterprise Java newsletter.