Tomcat classloading

M

Michael Mangeng

Hi

I´ve created a little class which loads data from a config file, creates
a URLClassloader, adds some urls, uses the new classloader to load a
startup-class and invokes the main(...) method. The advantage is that i
can define "path/to/*.jar" in the cfg file instead to mention each jar file.

This works fine with our application. All works perfectly.

The Problem:
We embedded Tomcat5.0.16 in our application and now after startup tomcat
complains that it cannot locate e.g. HttpServlet class.
(ClassNotFoundException). All .jar files supplied with tomcat-embedded
version can be loaded by the URLClassLoader (tomcat itself, servlet,jsp
classes,...) If this would not be the case i would not be able to
startup tomcat.

I think that the exception occours while trying to launch the first
webapplication.

I know that tomcat uses its own classloaders - can anybody explain why
such a problem may arise?

thanks,
mike
 
W

Wendy S

Michael Mangeng said:
I know that tomcat uses its own classloaders - can anybody explain why
such a problem may arise?

Tomcat constructs its own classpath from WEB-INF/classes and the contents of
WEB-INF/lib (in addition to global lib/classes directories). If you can
place your .jar files within the webapp, Tomcat will find them
automatically. Within the webapp, you can use getResourceAsStream to find
things in the classes directory, which is useful for storing .properties
files, etc., that your app may need.

Since you're already specifying a directory in which your app should find
all the .jar files, hopefully you can just move them to WEB-INF/lib, change
that config file, and everything will start working.
 
M

Michael Mangeng

Hi

Thanks for your reply but i am not allowed to place classes like
HttpServlet in the webapproot/web-inf/lib - it is forbidden by the spec.

besides that this is not the solution am searching for... i do not want
to distribute a jar more than one time in the installation.

greets,
mike
 
O

Oscar Kind

[ About successfully using a custom classloader ]
The Problem:
We embedded Tomcat5.0.16 in our application and now after startup tomcat
complains that it cannot locate e.g. HttpServlet class.
(ClassNotFoundException). All .jar files supplied with tomcat-embedded
version can be loaded by the URLClassLoader (tomcat itself, servlet,jsp
classes,...) If this would not be the case i would not be able to
startup tomcat.

Your custom classloader has nothing to do with Tomcat starting or not:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/class-loader-howto.html

I think that the exception occours while trying to launch the first
webapplication.

I know that tomcat uses its own classloaders - can anybody explain why
such a problem may arise?

It seems the new classloader doesn't defer to its parent classloader for
unknown classes. You did hook it up to Tomcat's classloader structure,
didn't you?


Oscar
 
M

Michael Mangeng

Hi Oscar,

thanks for your help!

No.. my bootstrapper does not have anything to do with the
tomcat-classloaders.

Indeed.. i´ve taken a look at the classloader structure... but i do not
get the mistake i made out of it...

The class-loading scheme is the following:
Step 1) The app is launched - at this time only the bootstrapper itself
and a config file for the bootstrapper is visible in the classpath.

Step 2) The bootstrapper creates a URLClassLoader and fills it with the
classes defined in the config file.

Step 3) The bootstrapper loads the class defined in the config file
with the brand new URLClassLoader and launches the application by
reflection - so it is not loaded by the system-classloader of the JDK
while loading the bootstrapper itself.

Step 4) The application starts up. in the startup procedure it launches
tomcat via its Embedded class, creates a host, context, engine. all (i
think) what is needed.

Step 5) I invoke embedded.start();

Step 6) Tomcat starts loading the webapps - after this a big exception
is thrown that the HttpServlet class cannot not be found
(ClassNotFoundException).


Because of step 3 - the classloader of the launcher class is the
urlclassloader. As far as i know - this classloader is handled to all
classes loaded by this class - thee are all except of the bootstrapper
itself.


greets,
mike

Oscar said:
[ About successfully using a custom classloader ]

The Problem:
We embedded Tomcat5.0.16 in our application and now after startup tomcat
complains that it cannot locate e.g. HttpServlet class.
(ClassNotFoundException). All .jar files supplied with tomcat-embedded
version can be loaded by the URLClassLoader (tomcat itself, servlet,jsp
classes,...) If this would not be the case i would not be able to
startup tomcat.


Your custom classloader has nothing to do with Tomcat starting or not:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/class-loader-howto.html


I think that the exception occours while trying to launch the first
webapplication.

I know that tomcat uses its own classloaders - can anybody explain why
such a problem may arise?


It seems the new classloader doesn't defer to its parent classloader for
unknown classes. You did hook it up to Tomcat's classloader structure,
didn't you?


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top