Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Java Classes Using Class.forName() to Load Groovy Classes

Avi Flax

2008-08-18

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi, I apologize if this is a FAQ, but I'm having trouble figuring this
out; I'd appreciate any pointers to help me out.

I'm using the Restlet framework [1] but I'll try to make that a
non-factor. I'm using Groovy 1.5.6 on an Intel Mac with Mac OS X
10.5.4, using Apple's 1.5.0_13-119 JVM.

My problem, essentially, is that I've got a Java class which knows the
name of a Groovy class, and it's trying to instantiate it with
Class.forName(), and I'm getting a ClassNotFoundException:

java.lang.ClassNotFoundException: com.arc90.slices.resources.AssignmentsResource
 at org.codehaus.groovy.tools.RootLoader.findClass (RootLoader.java:146)
 at java.lang.ClassLoader.loadClass (ClassLoader.java:316)
 at org.codehaus.groovy.tools.RootLoader.loadClass (RootLoader.java:118)
 at java.lang.ClassLoader.loadClass (ClassLoader.java:251)
 at java.lang.ClassLoader.loadClassInternal (ClassLoader.java:374)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName (Class.java:164)
 at org.restlet.ext.wadl.WadlApplication.attachResource(WadlApplication.java:267)

And here's line 267 of WadlApplication.java:
    final Class targetClass =
Class.forName(currentResource.getIdentifier());

currentResource.getIdentifier() just returns a string containing the
fully qualified name of a Groovy class.

I have no trouble instantiating the same class by name from within my
Groovy code, so I'm fairly certain that I don't have any obvious
classpath problems.

My basic question is: is there a way to effect class loading for
regular Java classes, so they can find Groovy classes when using
Class.forName()?

I'm beginning to suspect not, because Class.forName(name) doesn't even
work from within a Groovy script; I need to use Class.forName(name,
initialize, loader) and pass in the current class's loader.

If that's the answer, then I'm thinking what I'll want to do is modify
the Restlet class so I can pass a ClassLoader into it. Does that make
sense?

Thank you!!

[1] http://www.restlet.org/

--
Avi Flax » Lead Technologist » Partner » Arc90 » http://arc90.com
©2008 gg3721.com - Jax Systems, LLC, U.S.A.