Article View: borland.public.jbuilder.java.language
Article #14758Re: Loading classes / casting problem
From: Marcel van den Dungen
Date: Fri, 06 Aug 1999 00:00
Date: Fri, 06 Aug 1999 00:00
53 lines
2256 bytes
2256 bytes
maybe you have to cast your classes to implemented versions of befehl,
like for instance myCommand.
greetings
casper
Alex wrote:
> Hi,
>
> I�m trying to load some classes with my own classloader and cast them to an
> interface they implement but this causes a ClassCastException.
>
> Some details:
>
> I want an application to be able to execute commands that are represented by
> classes and who implement an interface called Befehl (german word for
> "command")
> This is what my interface looks like:
> package mypackage;
> public interface Befehl {
> public void execute();
> ...
> }
> Example for the "command-classes"
> package mypackage.befehle;
> public class MyCommand implements Befehl {
> public void someFunction () {...}
>
> public void execute () {...}
> }
>
> This is what I want to to:
> - first load a class from a certain directory (and resolve it)
> Class myClass = myClassLoader.load
> ("mypackage.befehle.myCommand"); -> seems to be working
> - cast the class to "Befehl" and save it (in Hashtable)
> Befehl b = (Befehl)
> -> throws ClassCastException !!!
>
> WHAT I REALLY DON�T UNDERSTAND:
> I wanted to be sure and tried this:
> Class test = myClass.getInterfaces()[0];
> System.out.println ("Interface: "+test.getName());
> I COULDN�T BELIEVE IT BUT THIS RETURNS: "Interface: Befehl"
> I don�t understand why the class tell�s me that it implements the interface
> "Befehl" but when I try to cast it into this interface an Exception is
> thrown.
>
> Please help, I don�t know where to search my mistake.
>
> Thank you very much,
> Alex.
Technical Details
Message-ID:
<37AACC80.85E24B37@c-content.nl>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7ocqkj$rm717@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f74b3,52cfadaf87143629 X-Google-Attributes: gidf74b3,public From: Marcel van den Dungen <info@c-content.nl> Subject: Re: Loading classes / casting problem Date: 1999/08/06 Message-ID: <37AACC80.85E24B37@c-content.nl> X-Deja-AN: 509557377 Content-Transfer-Encoding: 8bit References: <7ocqkj$rm717@forums.borland.com> Organization: C-CONTENT b.v. Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language