🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #14743

Loading classes / casting problem

#14743
From: Alex
Date: Thu, 05 Aug 1999 00:00
46 lines
2015 bytes
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: <7ocqkj$rm717@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f74b3,52cfadaf87143629,start
X-Google-Attributes: gidf74b3,public
From: "Alex" <alexbged@linux.zrz.tu-berlin.de>
Subject: Loading classes / casting problem
Date: 1999/08/05
Message-ID: <7ocqkj$rm717@forums.borland.com>
X-Deja-AN: 509303237
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
Organization: Another Netscape Collabra Server User
X-MSMail-Priority: Normal
Newsgroups: borland.public.jbuilder.java.language