Testing Spring+Hibernate application

My Jruby script is as follows:

require ‘java’
{ |jar| require jar;puts jar; }
require “c:/jruby-1.6.5/tejas.jar”
puts “hello done loading jars”

include_class “com.yantranet.platform.dao.AppDao”
include_class “com.yantranet.helpers.PageAndSize”

include_class “org.springframework.context.ApplicationContext”

def beans
[“DataSource”, “Hibernate”, “User” ].map { |c|
}.to_java :string
application_context = ClassPathXmlApplicationContext.new(beans)
app = application_context.getBean(“cleanAppDao”)
ps = PageAndSize.new(1,10)

The problem is with Java’s URLClassLoader that tries to instantiate a
SessionFactory bean which is defined thus:

  <property name="dataSource">
  <ref bean="dataSource" />

<property name="hibernateProperties">



<property name="configurationClass">

<property name="annotatedClasses">

<!-- <property name="configLocation"

value="/WEB-INF/spring/hibernate/hbm.cfg.xml"/> -->

<property name="mappingResources">

The exception when I run JRuby script is:

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘sessionFactory’ defined in URL
antra_server/webcontent/WEB-INF/spring/config/Hibernate.xml]: Invocation
of init
method failed; nested exception is java.lang.NoClassDefFoundError:
(root) at apptest.rb:41

I run the script as:

C:\jruby-1.6.5>jruby -J-cp c:\jruby-1.6.5\tejas.jar -Ic:\slf4j

I am trying to provide the slf4j-api-1.6.1.jar which has the
org.slf4j.LoggerFactory in various ways. Nothing seems to work so far.
It seems java is not getting the classpath for the slf4j jar.

I would appreciate your kind help.


what about putting the slf4j jars into the java classpath (-J-cp).
that should work.

  • Kristian

kristian wrote in post #1036223:

what about putting the slf4j jars into the java classpath (-J-cp).
that should work.

  • Kristian

Hi Kristian
I tried what you have suggested. Since JRuby uses Java’s URLClassLoader,
the classpath seems immaterial. I checked the URLClassLoader
documentation and it doesn’t seem to use class path. I tried the

F = java.io.File
class_loader = JRuby.runtime.jruby_class_loader


It loads the classes, but still Java can’t access the classes.


sorry for the late reply. what I do usually is not require any jars
within jruby and try to setup the classloader the java way. i.e. with
a webcontext I use the WEB-INF/lib. jruby will have the webcontext
classloader as parent and can import all the classes from there
without any require. basically I just want to make sure I have jar
only once in the classloader hierarchy (or the way up to the root).

outside of the webcontext jruby behaves a bit differently and there
either setup the classpath with ALL your jars via -J-cp or just use a
require script as you did which requires ALL jars (no jars on the
classpath) which should ensure that everything is coming from the same

if you know a little about maven you can have a look at
https://github.com/mkristian/dm-hibernate-adapter/tree/master/demo and
see how we setup the classpath there (mvn -X rails3:server)

hope that helps a bit further.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs