Digital Sanctum

software development, technology and other square topics

JRuby: Calling Ruby’s ActiveRecord from Java Using the Bean Scripting Framework

with 7 comments

JRuby: Calling Ruby’s ActiveRecord from Java Using the Bean Scripting FrameworkHere we need to use the Bean Scripting Framework (BSF) since Java 5 doesn’t have the built-in scripting support that Java 6 has. I will also use 2 helper classes (JRubyHelper.java and BSFHelper.java) provided by ociweb.com. For brevity I will leave out the source of the helper classes and concentrate on the actual code to make a call to a Ruby script from Java:

book_titles

Line 31: initializes an empty ArrayList that will be passed into the Ruby script.

Line 32: declares (or registers) the books by name which can then be referenced
in the Ruby script.

Line 33: actually executes the Ruby script.

Lines 36-40: just looping over the books and printing the titles.

Here I’ve created an arjava directory under the Rails plugins directory:

arjava_dir

book_titles.rb is the script called from Java:

book_titles


models.rb
defines the book Ruby object and extends ActiveRecord::base

models

query.rb is just a script to test the models.rb dependency.

query
For simplicity and just to prove the call to Ruby’s ActiveRecord will work from Java, I setup an Ant target to test it:

ant_arjava

That’s it for now. For a good write-up on ActiveRecord, Bruce Tate wrote an article on developerWorks: “Crossing borders: Exploring ActiveRecord”

Written by Shane

July 25th, 2007 at 11:45 am

7 Responses to 'JRuby: Calling Ruby’s ActiveRecord from Java Using the Bean Scripting Framework'

Subscribe to comments with RSS or TrackBack to 'JRuby: Calling Ruby’s ActiveRecord from Java Using the Bean Scripting Framework'.

Hi

is it possible to provide a working example for this article?

Thanks!

Jason

26 Jul 07 at 2:26 pm

I want to ask you about integration JRuby to Idea, without highlighting errors in: require ‘rubygems’

pftg

27 Jul 07 at 1:53 am

I’m using the latest version of the IDEA EAP IDE as well as the latest version of the Ruby plugin.

shane

27 Jul 07 at 11:22 am

Just tried your code. I had an error with connection to database. After knocking my head on the keyboard I found a mistake in your code (at least for me it was a mistake).
In the code ActiveRecord::Base.establish_connection … you wrote :user => “root” - well it should be :username => “root”.
The way I found out the error was - I ran the script and got a error for mysql connection - connection from root@192.168.*.* is forbidden. But I set :user => ‘my_correct_login’. So as :user was not valid option it was just ignored, so “root” was used instead by default.

Ziggy

22 Aug 07 at 8:10 am

[...] I found this great blog post that describes using the BSF to call Ruby’s ActiveRecord from [...]

I tried to run this example , but i am getting the following error
“unable to load language: ruby: org.jruby.exceptions.RaiseException: cannot load Java class JavaLang”

padmapriya

6 Aug 08 at 2:22 pm

I tried to run this example as
1) standalone
2)Using “ant run-java-ar”

In both cases i got the same above error
[java] org.jruby.exceptions.RaiseException: cannot load Java class JavaLang
[java] unable to load language: ruby: org.jruby.exceptions.RaiseException: cannot load Java class JavaLang

Please let me know if i need to set anything in the classpath?

padmapriya

6 Aug 08 at 2:45 pm

Leave a Reply


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser