Testing Spring+Hibernate application

My Jruby script is as follows:

require ‘java’
Dir[“c:/yantraeclipse/yantra_server/webcontent/web-inf/lib/*.jar”].each
{ |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”
include_class
“org.springframework.context.support.ClassPathXmlApplicationContext”
include_class
“org.springframework.context.support.FileSystemXmlApplicationContext”

def beans
[“DataSource”, “Hibernate”, “User” ].map { |c|
“file:c:/yantraeclipse/yantra_server/webcontent/WEB-INF/spring/config/#{c}.xml”
}.to_java :string
end
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>

<property name="hibernateProperties">
  <props>
    <prop

key=“hibernate.dialect”>org.hibernate.dialect.MySQLDialect
false

  </props>
</property>

<property name="configurationClass">
  <value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>

<property name="annotatedClasses">
  <list>
    <value>com.yantranet.model.DeviceProperty</value>
    <value>com.yantranet.model.DevicePropertyValue</value>
  </list>
</property>

<!-- <property name="configLocation"

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

<property name="mappingResources">
  <list>
    <value>com/yantranet/model/User.hbm.xml</value>
    <value>com/yantranet/model/Role.hbm.xml</value>
    <value>com/yantranet/model/App.hbm.xml</value>

The exception when I run JRuby script is:

NativeException:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘sessionFactory’ defined in URL
[file:c:/yantraeclipse/y
antra_server/webcontent/WEB-INF/spring/config/Hibernate.xml]: Invocation
of init
method failed; nested exception is java.lang.NoClassDefFoundError:
org/slf4j/Lo
ggerFactory
(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
apptest.rb

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.

Thanks
Murthy

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
following:

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

class_loader.add_url(F.new(‘c:\lib2’).to_url)
class_loader.add_url(F.new(‘slf4j-api-1.6.1.jar’).to_url)

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

Thanks
Murthy

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
classloader.

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

hope that helps a bit further.
-Kristian