I think that only the Solver class must be loaded by the same class loader.
Subclasses can be loaded by different ones. Make sure that the
URLClassLoader has logic to prevent Solver from being loaded from a URL.
The Solver class should be available using the findSystemClass() method in
the URLClassLoader, just like any other core jdk class. Code like this is
typically included in custom class loaders:
Class c = findSystemClass( className ) ;
if ( c != null ) {
return c ;
}
// Otherwise, load class using the custom loader
I believe Servlets are loaded with a custom class loader, which allows them
to do special things like automatic class re-loading if they change. If
Solver is in the same classpath as your servlet, try a different one. This
should force it to be loaded by the system class loader, probably the
default behavior of the Servlet class loader when it can't find the class in
the servlets directory.
Zol Heyman
-----Original Message-----
From: S.Ramaswamy [mailto:[EMAIL PROTECTED]]
Sent: Thursday, May 20, 1999 12:01 AM
To: [EMAIL PROTECTED]
Subject: Re: class casting from servlet
John Brecht wrote:
> Hello -
>
> I originally posted this on comp.lang.java.programmer, but
the responses
> I got seem to indicate that the problem has more to do
with this being a
> servlet, so I repost it here. (I'm brand new to this list.
I tried
> searching the archives, but the searcher just hung. Is
there an FAQ
> somewhere?)
>
> I am writing a servlet that acts as a dispatcher between
applets and
> classes on the server whose methods those applets need to
call. (These
> classes on the server should never get downloaded to the
client for
> security reasons.) All of these classes on the server
extend the same
> class, Solver. Solver has the method, checkAttempt(). Any
subclass of
> Solver is meant to override this method.
>
> My servlet uses a URLClassLoader to load the appropriate
class based on
> a string passed to it from the applet. I am able to get
the class
> loaded, but when I try to cast it to Solver, so I can call
it's
> checkAttempt() method, I get a ClassCastException. Here is
how I do the
> class loading:
>
> private Solver getSolver(String solverName) {
>
> Class solverClass;
> Solver theSolver = null;
>
> try {
> log("about to load class... "+solverName);
> solverClass = loader.loadClass(solverName);
> log("loaded class: "+solverClass);
> log("parent:"+solverClass.getSuperclass());
>
> log("about to instantiate...");
> Object theInstance = solverClass.newInstance();
> log("instantiated");
>
> if(theInstance instanceof Solver ) {
> log("about to cast as Solver...");
> theSolver = (Solver)(theInstance);
> log("cast solver:"+theSolver);
> } else {
> log("couldn't cast, not a Solver!");
> }
>
> } catch (Exception e) {
> log(""+e);
> }
>
> return theSolver;
> }
>
> "loader" is a URLClassLoader prepared with the appropriate
codebase upon
> initilization of the servlet. A check of the logs shows
that the class I'm
> trying to instantiate has Solver as a Superclass, but then
"theInstance"
> somehow does not end up being an actual instance of
Solver, and thus not
> castable to Solver. How can this be? What am I doing
wrong?
Classes loaded from two differenet classloaders cannot be
cast onto another.
Solution is that both the Solver and the other class needs
to be loaded by the
same Class Loader.
--
___________________________________________________________________________
S.Ramaswamy
Matrix Infotech Syndicate
D-7, Poorti, Vikaspuri, New Delhi, 110018, India
PHONE: +91-11-5610050, FAX: +91-11-5535103
WEB : http://MatrixInfotech.HyperMart.Net
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body
of the message "signoff SERVLET-INTEREST".
Archives:
http://archives.java.sun.com/archives/servlet-interest.html
Resources:
http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html