L
Luther Baker
Hi,
I've been walking through Stuart Halloway and Ted Newards' books on
Java component development - and can't seem to figure out how to
correctly cast a custom loaded class to a common interface.
Here is my dir structure:
classes/subdir/PointImpl.class
classes/IPoint.class
classes/PointClient.class
classes/PointServer.class
Here is my classpath:
CLASSPATH=/classes
Everything is explicitly in the classpath, except the Point
Implementation class. If I use this line:
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/") };
I get the following runtime exception:
java.lang.NoClassDefFoundError: Point
Whereas, if I use this line:
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/"), new URL ("file:classes/") };
I get the following runtime exception:
Exception: java.lang.ClassCastException
I would guess that the URLClassLoader needs to see the IPoint.class
interface, so it makes sense to include the additional path (although
Stuart's example does not), but when I add the the classes directory
to the URLClassLoader classpath, it correctly instantiates PointImpl,
but cannot cast it since the primordial and custom class loaders see
two unique IPoint interfaces.
Looking for suggestions on how to correctly cast to a common
interface, when using different class loaders ... or at least, some
insight into what Stuart implies can be done.
Thanks,
-Luther
import java.net.*;
public class PointServer
{
static ClassLoader cl_;
static Class ptClass_;
public static synchronized Point
createPoint (Point template)
throws Exception
{
if (ptClass_ == null) {
PointServer.reloadImpl ();
}
//// CLASS CAST EXCEPTION OCCURS HERE
Point newPt = (Point) ptClass_.newInstance ();
if (template != null) {
newPt.move (template.getX(), template.getY());
}
return newPt;
}
public static synchronized
void reloadImpl ()
throws Exception
{
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/"), new URL ("file:classes/") };
Class localClass = PointServer.class.getClass ();
ClassLoader localClassLoader = localClass.getClassLoader ();
cl_ = new URLClassLoader (serverURLs, localClassLoader);
ptClass_ = cl_.loadClass ("PointImpl");
}
}
I've been walking through Stuart Halloway and Ted Newards' books on
Java component development - and can't seem to figure out how to
correctly cast a custom loaded class to a common interface.
Here is my dir structure:
classes/subdir/PointImpl.class
classes/IPoint.class
classes/PointClient.class
classes/PointServer.class
Here is my classpath:
CLASSPATH=/classes
Everything is explicitly in the classpath, except the Point
Implementation class. If I use this line:
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/") };
I get the following runtime exception:
java.lang.NoClassDefFoundError: Point
Whereas, if I use this line:
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/"), new URL ("file:classes/") };
I get the following runtime exception:
Exception: java.lang.ClassCastException
I would guess that the URLClassLoader needs to see the IPoint.class
interface, so it makes sense to include the additional path (although
Stuart's example does not), but when I add the the classes directory
to the URLClassLoader classpath, it correctly instantiates PointImpl,
but cannot cast it since the primordial and custom class loaders see
two unique IPoint interfaces.
Looking for suggestions on how to correctly cast to a common
interface, when using different class loaders ... or at least, some
insight into what Stuart implies can be done.
Thanks,
-Luther
import java.net.*;
public class PointServer
{
static ClassLoader cl_;
static Class ptClass_;
public static synchronized Point
createPoint (Point template)
throws Exception
{
if (ptClass_ == null) {
PointServer.reloadImpl ();
}
//// CLASS CAST EXCEPTION OCCURS HERE
Point newPt = (Point) ptClass_.newInstance ();
if (template != null) {
newPt.move (template.getX(), template.getY());
}
return newPt;
}
public static synchronized
void reloadImpl ()
throws Exception
{
URL[] serverURLs = new URL[] { new URL
("file:classes/subdir/"), new URL ("file:classes/") };
Class localClass = PointServer.class.getClass ();
ClassLoader localClassLoader = localClass.getClassLoader ();
cl_ = new URLClassLoader (serverURLs, localClassLoader);
ptClass_ = cl_.loadClass ("PointImpl");
}
}