Tutorials for Hibernate, EJB 2, EJB 3
Struts, JavaServerfaces (JSF)
Tomcat, JBoss, Myeclipse, Eclipse and other

Tutorials and Articles » Java, JRuby, Testing and Tools » Java and JRuby working together

Sprache / Language

Navigation
Java and JRuby working together 

Collection of issues and solutions I encountered when using JRuby. Further information can be found in the JRuby wiki.

Importing Java classes into JRuby

To prevent overwriting of other classes you should consider to put Java classes into a separate name space. A namespace is just a Ruby module.

module Java
 include_class 'java.util.ArrayList'
 include_class 'de.laliluna.Foo'
end

Now you can use the Java classes like

list = Java::ArrayList.new

Hint: If you don't include a java class, you can always use it by adding Java:: and the full package name.

list = Java::JavaUtil::ArrayList.new

Java enumeration / enum

public class ShopOrder {  
    public enum Status {
        OPEN, CANCELLED, DELIVERED
    }
}

Getting a Java enum from a Ruby symbol

def getJavaEnum symbol = :open
  Java::ShopOrder::Status.value_of(status.to_s.upcase)
end

java.util.List, java.util.ArrayList

To use a java.util.List I found it easiest to convert it to a Ruby structure, i.e. an array.

The following code shows how to remove duplicate entries from a List. You can run the code in jirb (this is Jruby console, just type jirb in a terminal to start it)

list = Java::JavaUtil::ArrayList.new
 list.add 1
 list.add 2
 list.add 1

Instead of add you can use the more Ruby like

list << 4

Print the list

list
=> #<Java::JavaUtil::ArrayList:0x1082823 @java_object=[1, 2, 3]>

Convert it to a Ruby array and remove duplicated entries

list.to_a.uniq
=> [1, 2]

Exception handling

When calling Java code from JRuby, you will get Java exceptions wrapped in a NativeException.

The following code shows how to catch the exception and to extract the wrapped exception.

begin
buffer = @accountService.download(session[:username], Integer(item_pk))
rescue NativeException => e
if (e.cause.kind_of?(Java::JavaLang::SecurityException))
flashError 'You can't download other peoples stuff'
redirect R(self, :index)
end
end




Java Persistence and Hibernate Training

I offer training on Hibernate and Java Persistence (Glassfish with Oracle Toplink or JBoss).
Get more information.

Consulting / Development

I am a freelancer and can be booked for your projects. In cooperation with other specialists, you have even access to a development team of highly qualified experts.
Get more information.

Hibernate and Java Persistence Developer Guide
Hibernate Buch cover klein

XML and Annotations, Performance chapter, Hibernate search, Integration with Spring, EJB3 and JSF
Available as up to date ebook
Get more information.

News 
Devoxx 2009 Presentation (Nov. 18, 2009)
Window Id Browser Extension (Blog) (Nov. 13, 2009)
JSF 2 evaluation and test (Blog) (Nov. 10, 2009)
Devoxx presentation on Choosing Web Frameworks (Blog) (Sep. 17, 2009)
JQuery JSON autocomplete library (Blog) (Sep. 01, 2009)
Better Java application packaging - JSR 277 Java Module System (Blog) (Aug. 21, 2009)
Performance Tuning Tips for Hibernate and Java Persistence (Jan. 27, 2009)
Hibernate / Java Persistence eBook update (Jan. 27, 2009)
Hibernate Search with Lucene (Jan. 12, 2009)
Notes from the Devoxx Conference (Dec. 16, 2008)
Copyright (c) 2004-2009 by Sebastian Hennebrueder, laliluna.de Impressum