[play-framework] do modules use a separate class loader?

156 views
Skip to first unread message

Mike Grove

unread,
May 14, 2010, 11:51:28 AM5/14/10
to play-fr...@googlegroups.com
I have a module which contains a plugin -- when starting up the plugin reads a file on disk, which contains a list of full qualified class names and it retrieves these via Class.forName to do some operations with the class files.  The class names are all in the path of the main play application, they're not in the path of the module.

When I run everything from my IDE, everything works fine.  When I build out the app from my build script and run it via the play script, I get class not found errors.  It would seem like the module is using its own class loader that does not have access to the classes from the main play app, is that the case or am i jumping to the wrong conclusion?

Thanks.

Mike

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Nicolas Leroux

unread,
May 14, 2010, 1:37:00 PM5/14/10
to play-fr...@googlegroups.com
Play and the modules have their own classloader. I think you will need to do something like:

Thread.currentThread().setContextClassLoader(Play.classloader);

Hope this helps,

Nicolas

Guillaume Bort

unread,
May 14, 2010, 2:29:06 PM5/14/10
to play-fr...@googlegroups.com
Or just uses Play.classLoader.loadClass(...) instead of Class.forName(...).

Mike Grove

unread,
May 14, 2010, 3:30:58 PM5/14/10
to play-fr...@googlegroups.com
On Fri, May 14, 2010 at 2:29 PM, Guillaume Bort <guillau...@gmail.com> wrote:
Or just uses Play.classLoader.loadClass(...) instead of Class.forName(...).

I thought about this, but the module uses a separate library, which is where the calls to Class.forName come from, so it won't have any handle to the Play core.
 

Guillaume Bort

unread,
May 16, 2010, 2:57:20 PM5/16/10
to play-fr...@googlegroups.com
I'm not sure but perhaps that Class.forName will use the context
classloader. So setting setContextClassLoader as Nicolas suggested
could help.

Mike Grove

unread,
May 17, 2010, 7:50:59 AM5/17/10
to play-fr...@googlegroups.com
On Sun, May 16, 2010 at 2:57 PM, Guillaume Bort <guillau...@gmail.com> wrote:
I'm not sure but perhaps that Class.forName will use the context
classloader. So setting setContextClassLoader as Nicolas suggested
could help.

Yeah, this is basically what I ended up doing, I set the class loader on the thread context, and I made a utility function i the module that would try Class.forName first, and if I get a class not found from that, I checked the current thread context class loader, and if that doesnt work, then I try the system class loader.  If that fails, I rethrow the class not found exception.

Thanks.

Mikle
 
Reply all
Reply to author
Forward
0 new messages