tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vladimir Grishchenko <>
Subject RE: Dynamic Class Loading - Reflexion failure
Date Thu, 02 Aug 2001 16:20:16 GMT

> -----Original Message-----
> From: Frank Bourdache 
> []
> Sent: Thursday, August 02, 2001 12:45 AM
> To:
> Subject: Dynamic Class Loading - Reflexion failure
> Hi,
> I am facing problems while loading dynamically classes in my 
> servlet. For my application's purposes, I have to generate 
> java code and compile it on the fly, then use it.
> Using standard reflexion mecanisms, I try to instanciate a 
> new testClass object. If it fails, I fork a new process to 
> generate the class, and retry to instanciate this object again.
> If testClass.class is not present when I'm starting Tomcat 
> 4.0b6, there is no way i can instanciate a testClass Object. 
> (exceptions are raised in both blocks)

ok, see comments below

> If I comment out the first block (I always generate the 
> testClass.class file), then everything works fine, but this 
> is unacceptable.

If this works the first scenario should work too. Are you sure
you attempt to instantiate a new class after .class file is actually
generated and not prematurely?

After you figure out what is the problem you might consider compling
bytecodes without forking a new process and avoid the unnecessary overhead.
That's how jsp are compiled in tomcat (or JaSPER?).

Also my impression is that Tomcat (at least 3.2) reloads all the classes 
in web application regardless of which class has changed, so if you
generate a new .class and try to instantiate it through webapp class
loader it will cause all classes in webapp to reload (correct me if
I'm wrong) = overhead. What you could do instead is to use an instance of
URLClassLoader to load your generated file. Keep in mind that if you
decide to do that you'll have to put your generated class outside of
system classpath.
If you put it on the system classpath no class reloading is possible.
The problem with URLClassLoader approach is that your newly generated
class will not be able to find any of the classes in your webabapp 
No matter what you do it's not gonna be pretty, unless the class
you're trying to reload is pretty much isolated.


> I also tried using classLoader, but failed again.
> Don't know if it really is Tomcat related. Any clues ?
> Thanks for the help !
> Regards,
> /Frank
> public class LoadClassSample extends HttpServlet {
>         ...
>         try {
>             Class testObjClass = Class.forName("testClass");
>             Object testObject = testObjClass.newInstance();
>         } catch( ClassNotFoundException CNFException ) {
>             try {
>                 Process _proc = 
> Runtime.getRuntime().exec("/usr/lib/java/bin/javac 
> /tmp/ -d 
> /usr/local/jakarta-tomcat-4.0-b6/webapps/examples/WEB-INF/classes/");
>                 _proc.waitFor();
>                 Class testObjClass = Class.forName("testClass");
>                 Object testObject = testObjClass.newInstance();
>                 ...                
>             } catch( Exception PROCException ) {
>                 ...
>             }
>         ...

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. Any unauthorized review, use, disclosure or distribution
is prohibited. If you are not the intended recipient, please contact
the sender by reply e-mail and destroy all copies of the original

View raw message