JRuby is a pretty good Java implementation of Ruby. One of the biggest advantages of JRuby is that you can use Java Classes in your Ruby on Rails App. You can import JARs and take advantages of all the Java Libraries and Frameworks. In that way you don’t have to throw away your old Java Code. You can build a JAR and use it inside your new fancy Ruby App.
I recommend to build a Uber JAR, which contains all dependencies of your Java App. That is more easy to handle. You have to put your Uber JAR into the “lib” directory of your rails app. Than you have to import the JAR in the “application.rb”.
That’s it! Know you can access the Classes inside the JAR. If you want to use a Java Class in a Controller you have to include the class:
include_class Java::org.trophic.graph.service.LocationServiceImpl include_class Java::org.trophic.graph.factory.LocationFactory
And here is how you can use the Java Classes:
locationService = LocationFactory.locationService locations = locationService.locations
And here is the full example of the controller:
class PageController < ApplicationController include_class Java::org.trophic.graph.service.LocationServiceImpl include_class Java::org.trophic.graph.factory.LocationFactory def home locationService = LocationFactory.locationService @locations = locationService.locations end end
It is super easy! Isn’t it?