NoClassDefFoundError when using URLClassLoader

Hi,

I have an applet which tries to load a jar file dynamically. I am giving the code snippet here.

loader = new URLClassLoader(new URL[] { new URL("http://localhost:4095/jars/dynamicload.jar") });

m_class = loader.loadClass("loader.DynamicLoadedClass");

(loader.DynamicLoadedClass) prntr = (loader.DynamicLoadedClass) m_class.newInstance();

But this is throwing a NoClassDefFoundError when I try to cast the new instance to a DynamicLoadedClass.

Can anybody help me correct this problem?

Thanks

Unni

[557 byte] By [Unnikrishnan.P.Sa] at [2007-10-2 16:50:51]
# 1

It will be happening when your code tries to access loader.DynamicLoadedClass for the cast. When a class accesses another class implicitly it asks the ClassLoader that loaded it for the class. That will presumably be the standard class loader that points to the classpath, and knows nothing about your dynamically loaded class.

Really if you mention a class to the compiler then there's no point in trying to load it dynamically, since you must already have it locally to do the compilation.

Usually you know that an dynamically loaded class will conform to some abstract class or interface you have locally, and you cast the new instance to that. That works because standard classloader delegation means your URLClassLoader will take classes from the class path if they are there.

malcolmmca at 2007-7-13 18:02:30 > top of Java-index,Java Essentials,Java Programming...