tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Kehoe <ke...@fortuity.com>
Subject Re: can't dynamically load a class?
Date Fri, 25 Feb 2000 02:23:51 GMT
Just to follow up, here's what was needed:

String dynamicReportName = "com.myCompany.myApp.SpecificReport";
myReport = (myReportInterface)Class.forName(dynamicReportName).newInstance();

It is necessary to specify the complete class name (the full package 
name and class name). For anyone else moving existing servlets to 
Tomcat, this is different from JRun and JServ.

My thanks to Randall Parker for the solution!

Daniel
kehoe@fortuity.com

At 1:30 PM -0800 2/16/00, Daniel Kehoe wrote:
>At 12:35 PM -0700 2/16/00, Danno Ferrin wrote:
>>With the new context specs in Servlet 2.2 some different things are
>>required to happen with classloader, such as each context has a separate
>>classloader instance.
>>
>>try Class.forName("foo", this.getClass().getClassLoader())
>
>The syntax is
>Class.forName(String name, boolean initialize, ClassLoader loader)
>so I tried
>myReport = (myReportInterface)Class.forName(dynamicReportName, true, 
>this.getClass().getClassLoader()).newInstance();
>but I still get ClassNotFoundException.
>
>Is it known to work?
>
>Daniel
>>kehoe@fortuity.com
>
>
>
>>Daniel Kehoe wrote:
>> >
>> > I'm trying to use the method Class.forName in a servlet and it
>> > doesn't work with Tomcat (worked fine with JServ and Jrun). I get a
>> > ClassNotFoundException. It should work, right?
>> >
>> > Daniel
>> > kehoe@fortuity.com
>> >
>> > >Date: Mon, 14 Feb 2000 16:47:17 -0800
>> > >To: tomcat-dev@jakarta.apache.org
>> > >From: Daniel Kehoe <kehoe@fortuity.com>
>> > >Subject: why ClassNotFoundException on attempt to dynamically 
>>load a class?
>> > >
>> > >Why does this code throw an exception
>> > >"java.lang.ClassNotFoundException: SpecificReport"?
>> > >
>> > >String dynamicReportName = "SpecificReport";
>> > >myReport = 
>>(myReportInterface)Class.forName(dynamicReportName).newInstance();
>> > >
>> > >It works fine if I just instantiate the object:
>> > >SpecificReport aSpecificReport = new SpecificReport();
>> > >
>> > >And it works fine under Jserv or Jrun.
>> > >
>> > >I hear someone asking, why does he want to do this? Answer: I want
>> > >to load a class dynamically based on a parameter passed from the
>> > >browser, like:
>> > >String dynamicReportName = request.getParameterValues("report")[0];
>> > >
>> > >I'm using Tomcat 3.1M1 with java 1.2.2 Classic VM (build
>> > >Linux_JDK_RC3, native threads, sunwjit). What's going on that's
>> > >different?
>> > >
>> > >Thanks!
>> > >


Mime
View raw message