classloading question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

classloading question

Resolve Test
I use a library that creates objects using Class.forName(..).newInstance(). When it tries to create objects for classes defined under grails' src/java it fails with java.lang.ClassNotFoundException.

Any suggestions?

Thanks
Duke
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: classloading question

Resolve Test
Looks like I'm able to get to the class with the following:

Class clazz = Class.forName("com.resolve.rscore.MInfo", true, Thread.currentThread().getContextClassLoader());

However now I'm getting the following when creating the object with clazz.newInstance() from within the library jar

java.lang.IllegalAccessException: Class com.resolve.esb.DefaultHandler can not access a member of class com.resolve.rscore.MInfo with modifiers ""
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
        at java.lang.Class.newInstance0(Class.java:344)
        at java.lang.Class.newInstance(Class.java:303)

Any ideas on what would causes the IllegalAccessException?
I dont get the exception doing it from a grails service.

Thanks
Duke

Duke wrote
I use a library that creates objects using Class.forName(..).newInstance(). When it tries to create objects for classes defined under grails' src/java it fails with java.lang.ClassNotFoundException.

Any suggestions?

Thanks
Duke
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: classloading question

buck84
Hi,

I experience absolutely the same problem.

I have a java class in src/java which uses some library and pass to this library another src/java class which is dynamically loaded.

After a small investigation I found out, that:
1. Grails classloading mechanism looks like this GrailsClassLoader -> UrlClassLoader -> GrailsRootLoader -> AppClassloader (-> = parent)
2. all src/java classes are loaded by UrlClassLoader, all library classes (at least using jetty, didn't check war+tomcat deployment) are loaded by GrailsRootLoader
3. Then according to classloading rules in Java2 it makes all src/java classes invisible to libraries, because when trying to dynamically invoke from library class some src/java class, GrailsRootLoader (this classloader is used because library class was loaded by it) first asks its parent AppClassLoader and fails, then tries to load class itself looking in libraries folders and fails again.

It seems like current archecture allows only src/java classes to use library classes and not vice versa. Does anyone know how to hack around this problem?

Duke wrote
Looks like I'm able to get to the class with the following:

Class clazz = Class.forName("com.resolve.rscore.MInfo", true, Thread.currentThread().getContextClassLoader());

However now I'm getting the following when creating the object with clazz.newInstance() from within the library jar

java.lang.IllegalAccessException: Class com.resolve.esb.DefaultHandler can not access a member of class com.resolve.rscore.MInfo with modifiers ""
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
        at java.lang.Class.newInstance0(Class.java:344)
        at java.lang.Class.newInstance(Class.java:303)

Any ideas on what would causes the IllegalAccessException?
I dont get the exception doing it from a grails service.

Thanks
Duke

Duke wrote
I use a library that creates objects using Class.forName(..).newInstance(). When it tries to create objects for classes defined under grails' src/java it fails with java.lang.ClassNotFoundException.

Any suggestions?

Thanks
Duke
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: classloading question

mattlary
I'm having the same problem also, yet I am unable to change the library code as mentioned below; is there any way to get around this problem other than by enclosing my classes which need to be accessed by the jar in their own jar?  That's the only way I can think of to fix the issue.

Thanks,

-Matt

buck84 wrote
Hi,

I experience absolutely the same problem.

I have a java class in src/java which uses some library and pass to this library another src/java class which is dynamically loaded.

After a small investigation I found out, that:
1. Grails classloading mechanism looks like this GrailsClassLoader -> UrlClassLoader -> GrailsRootLoader -> AppClassloader (-> = parent)
2. all src/java classes are loaded by UrlClassLoader, all library classes (at least using jetty, didn't check war+tomcat deployment) are loaded by GrailsRootLoader
3. Then according to classloading rules in Java2 it makes all src/java classes invisible to libraries, because when trying to dynamically invoke from library class some src/java class, GrailsRootLoader (this classloader is used because library class was loaded by it) first asks its parent AppClassLoader and fails, then tries to load class itself looking in libraries folders and fails again.

It seems like current archecture allows only src/java classes to use library classes and not vice versa. Does anyone know how to hack around this problem?
Duke wrote
More info from "Duke"
Looks like I'm able to get to the class with the following:

Class clazz = Class.forName("com.resolve.rscore.MInfo", true, Thread.currentThread().getContextClassLoader());

However now I'm getting the following when creating the object with clazz.newInstance() from within the library jar

java.lang.IllegalAccessException: Class com.resolve.esb.DefaultHandler can not access a member of class com.resolve.rscore.MInfo with modifiers ""
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
        at java.lang.Class.newInstance0(Class.java:344)
        at java.lang.Class.newInstance(Class.java:303)

Any ideas on what would causes the IllegalAccessException?
I dont get the exception doing it from a grails service.

Thanks
Duke
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: classloading question

buck84
I know it's stupid but I'm afraid this is the only way to do it.

And here are official comments on this issue: http://jira.codehaus.org/browse/GRAILS-1879

Well I'm not a big expert in classloaders, but I wonder why everything works in tomcat and doesn't in jetty. BTW never experienced such problems with jetty using other web frameworks.

mattlary wrote
I'm having the same problem also, yet I am unable to change the library code as mentioned below; is there any way to get around this problem other than by enclosing my classes which need to be accessed by the jar in their own jar?  That's the only way I can think of to fix the issue.

Thanks,

-Matt

buck84 wrote
Hi,

I experience absolutely the same problem.

I have a java class in src/java which uses some library and pass to this library another src/java class which is dynamically loaded.

After a small investigation I found out, that:
1. Grails classloading mechanism looks like this GrailsClassLoader -> UrlClassLoader -> GrailsRootLoader -> AppClassloader (-> = parent)
2. all src/java classes are loaded by UrlClassLoader, all library classes (at least using jetty, didn't check war+tomcat deployment) are loaded by GrailsRootLoader
3. Then according to classloading rules in Java2 it makes all src/java classes invisible to libraries, because when trying to dynamically invoke from library class some src/java class, GrailsRootLoader (this classloader is used because library class was loaded by it) first asks its parent AppClassLoader and fails, then tries to load class itself looking in libraries folders and fails again.

It seems like current archecture allows only src/java classes to use library classes and not vice versa. Does anyone know how to hack around this problem?
Duke wrote
More info from "Duke"
Looks like I'm able to get to the class with the following:

Class clazz = Class.forName("com.resolve.rscore.MInfo", true, Thread.currentThread().getContextClassLoader());

However now I'm getting the following when creating the object with clazz.newInstance() from within the library jar

java.lang.IllegalAccessException: Class com.resolve.esb.DefaultHandler can not access a member of class com.resolve.rscore.MInfo with modifiers ""
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
        at java.lang.Class.newInstance0(Class.java:344)
        at java.lang.Class.newInstance(Class.java:303)

Any ideas on what would causes the IllegalAccessException?
I dont get the exception doing it from a grails service.

Thanks
Duke
Loading...