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