Preface

JRuby is just Ruby taking advantage of Java’s VM; taking the suck out of Java and putting some extra awesome into Ruby.

—Charles Nutter, JRuby project lead Twitter, August 7, 2008

And with that quote, Charles Nutter summarizes the two forces that have recently brought attention to the JRuby project: the recognition that Java provides a powerful platform that can be used by languages other than Java, and the increase in interest in the Ruby programming language. In the recipes ahead, we will explore a wide variety of usage scenarios for JRuby. In Charles’s terms, some recipes are about taking the suck out of Java, some are about putting some extra awesome into Ruby, and some are about both.

Audience

To fully leverage JRuby, you must be able to move freely between the Java and Ruby domains. In writing the JRuby Cookbook, we had in mind a reader with some understanding of both languages, possibly with a better understanding of one or the other. As a result, you won’t find a lot of basic introductory material, save for the first chapter where we illustrate the areas where Ruby and Java are similar as well as where they differ.

Our overall approach is that the purpose of the recipes in this book is not to educate you on some preexisting Java or Ruby capability, but instead to explain how to use JRuby within the context of, or as an enhancement to, these existing capabilities. For example, the recipes in the JRuby on Rails chapter are written for someone who has already created a (working) Rails application.

Organization

Chapter 1, Getting Started

This chapter starts off with a brief introduction to JRuby before stepping through a number of basic usages of JRuby, including how to use the RubyGems package management system and how to interact with Java code from Ruby code. The package concludes with a number of recipes about setting up various integrated development environments (IDEs) for working with JRuby.

Chapter 2, JRuby on Rails

This chapter is focused on a variety of scenarios for deploying Ruby on Rails applications using JRuby.

Chapter 3, Java Integration

This chapter starts with several recipes about invoking Ruby code from Java code and then continues into recipes describing the usage of popular Java libraries such as Java Native Access ( JNA) and Jakarta Commons Logging from Ruby.

Chapter 4, Enterprise Java

The recipes in this chapter are all about using JRuby with enterprise Java frameworks such as JMS, JNDI, EJB, Spring, and Hibernate.

Chapter 5, User Interface and Graphics

This chapter describes a number of JRuby-based frameworks that facilitate the creation of user interfaces. It also includes recipes about image manipulation, applets, and desktop integration.

Chapter 6, Build Tools

The recipes in this chapter are focused on using JRuby to enhance the build process of your Java project. Ant and Maven, the two most popular Java-based build tools, both have several different ways that JRuby can be used. There are also recipes about the JRuby-specific build tools Raven and Buildr.

Chapter 7, Testing

The focus of this chapter is on JtestR, a package that includes JRuby and a variety of popular Ruby testing tools. Through the recipes in this chapter, you will learn how to write Ruby-based tests of Java code.

Chapter 8, The JRuby Community

This final chapter includes a few recipes about effectively participating in the JRuby community.

Conventions Used in This Book

This book uses the following typographic conventions:

Italic

Used for example URLs, names of directories and files, options, and occasionally for emphasis.

Constant width

Used for program listings. Also used within paragraphs to refer to program elements such as namespaces, classes, and method names.

Constant width italic

Indicates text that should be replaced with user-supplied values.

Note

This icon indicates a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “JRuby Cookbook, by Justin Edelson and Henry Liu. Copyright 2009 Justin Edelson and Henry Liu, 978-0-596-51980-3.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

When you see a Safari® Books Online icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

Comments and Questions

We at O’Reilly have tested and verified the information in this book to the best of our ability, but mistakes and oversights do occur. Please let us know about errors you may find, as well as your suggestions for future editions, by writing to:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the U.S. or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

To ask technical questions or comment on the book, send email to:

We have a website for this book where examples, errata, and any plans for future editions are listed. You can access this site at:

http://www.oreilly.com/catalog/9780596519803

For more information about this book and others, see the O’Reilly website:

http://www.oreilly.com

Acknowledgments

Thanks to the O’Reilly staff, especially our editor Mike Loukides and copyeditor Colleen Gorman. Thanks to Steven Shingler for his contribution to Chapter 4. And thanks to all who reviewed this book including Juan Pablo Tarquino, John Purcell, and David Koontz.

This book simply would not and could not exist without the tireless efforts of the whole JRuby project team, including Charles Nutter, Thomas Enebo, Nick Sieger, and Ola Bini. Thanks also to Sun and ThoughtWorks for their ongoing support of JRuby. The JRuby project is hosted by The Codehaus; thanks as well to Bob McWhirter for his work there.

We both would like to thank Nick Rockwell for his ongoing encouragement and enthusiasm.

Justin Edelson

This book wouldn’t have happened without the love and support of my wonderful wife, Elizabeth. Special thanks to my sons: Owen, who typed his name all by himself, and Benjamin, who can’t yet.

Thanks to my team at MTV Networks: Michael Benoit, Keith Griffin, Ramesh Nuthalapati, Ilya Reznikov, Chris Sindel, Jeff Yemin, and Jun Zhou, for all their hard work. Thanks also to Warren Habib for his support.

Henry Liu

Thanks to my friend Jon Baer for inviting me to my first Ruby meeting and being a great collaborator throughout the years. I’m grateful to Francis Hwang, Matt Pelletier, Sebastian Delmont, Trotter Cashion, and all the members of the NYC Ruby group. They taught me Ruby and Rails by answering all my newbie questions, and it was their passion and enthusiasm for the technology that motivated me to dig deeper. Thanks to all my colleagues at MTV Networks and specifically Mark Ache, Luke Murphy, and Steve Azueta for their continued support. Most of all, thanks to my family and my partner, Naomi; without her, none of this would be possible.

Get JRuby Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.