Classloaders (I think)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Classloaders (I think)

zeusfaber
Hi all,
I'm moving a web application from Tomcat to Jetty 5.0 I use a self
made FrontController implementation (Struts style but much simpler).
I have the Main Servlet (similar the Struts ActionServlet) into the
WEB-INF/lib folder.
This servlet loads a properties file containing the actions via:

actionMappings = new Properties();
actionMappings.load(FrontController.class.getClassLoader().getResourceAsStream(commandFileName);

In tomcat it works but in Jetty I can load the properties file only via:

ServletContext.getResourceAsStream(commandFileName);

Why the classic class.getClassLoader() doesn't work anymore?

Going further, Receiving a request, my RequestProcessor class load a
new Action class via:

Class clazz = Class.forName(ActionClassName);

The superclass of the Action is in the WEB-INF/lib but all the
application specific subclasses are into the WEB-INF/classes folder.

Also here I'm getting a ClassNotFoundException...

Of course, I move everything into the WEB-INF/classes folder everything is fine.

Could you explain me why I cannot load a class located into the
WEB-INF/classes folder executing the call from within the WEB-INF/lib
folder.
Is there a way to circumvent thsi problem so I can have the same
packaging under tomcat and jetty?

Thank you in advance

--
Davide


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Classloaders (I think)

Greg Wilkins-5
David DB wrote:

> Hi all,
> I'm moving a web application from Tomcat to Jetty 5.0 I use a self
> made FrontController implementation (Struts style but much simpler).
> I have the Main Servlet (similar the Struts ActionServlet) into the
> WEB-INF/lib folder.
> This servlet loads a properties file containing the actions via:
>
> actionMappings = new Properties();
> actionMappings.load(FrontController.class.getClassLoader().getResourceAsStream(commandFileName);
>
> In tomcat it works but in Jetty I can load the properties file only via:
>
> ServletContext.getResourceAsStream(commandFileName);
>
> Why the classic class.getClassLoader() doesn't work anymore?

Can you print out / log   FrontController.class.getClassLoader().
Is that the system classloader or the context classloader?

I suspect you may have your class in two places, but your properties
file in only one?

Anyway, once we know who's classloader we are dealing with, this will
be easy to resolve.


> Going further, Receiving a request, my RequestProcessor class load a
> new Action class via:
>
> Class clazz = Class.forName(ActionClassName);
>

Ah the evil Class.forName!!!!!

This goes straight to the system classloader, so unless you have
all your classes on the system classpath it will definitely fail.
Even then if you have your classes in WEB-INF/* as well, you may
still have problems with the inverted class loader hierarchy
that the servlet spec requires.

Anyway, you should use the current threads context classloader
to load classes from the context classpath.


> The superclass of the Action is in the WEB-INF/lib but all the
> application specific subclasses are into the WEB-INF/classes folder.
>
> Also here I'm getting a ClassNotFoundException...
>
> Of course, I move everything into the WEB-INF/classes folder everything is fine.

Mmmm amazed that works!  just makes me think that you really do have
you classes in more than one location!


> Could you explain me why I cannot load a class located into the
> WEB-INF/classes folder executing the call from within the WEB-INF/lib
> folder.
> Is there a way to circumvent thsi problem so I can have the same
> packaging under tomcat and jetty?



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support