NoClassDefFoundError in an ClassLoader for a class already loaded ?

V

veovis

I use an extended URLClassLoader to load classes from directories not
included in classpath. Everything works well the following way:

package A: Included in claspath. To it belong: a starting class Start,
the classloader Loader, and some other classes.

package B: Not included in classpath. To it belong (for this
explanation purpose): classes Master, Slave, AbstractFoo, and Foo
which extends AbstractFoo.

packageA.Start instantiates packageA.Loader, loader instantiates
packageB.Master within a thread, master creates a new Slave wthich in
turn creates new Foo() as well other objects from inside other
packages (package A and others..) Everything's fine.

BUT: let's assume in package A exists class LocalFoo which extends
whichever packageB.Foo or packageB.AbstractFoo (packageB is not
included in classpath, remember?).

When slave instantiates packageA.LocalFoo, following exception is
generated:
java.lang.NoClassDefFoundError: packageB/AbstractFoo
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at PackageB.Slave.doSomething(Slave.java:31)
at PackageB.Master.doSomething(Master.java:19)
at PackageB.Master.run(Master.java:14)

Why? What am missing that is very obvious or silly?
(NOTE: hope explanation above is enough; but I will paste a bit of
code on demand.)
Thanks !
 
D

Dave Glasser

(e-mail address removed) (veovis) wrote on 6 Apr 2004 19:21:34 -0700
in comp.lang.java.programmer:
I use an extended URLClassLoader to load classes from directories not
included in classpath. Everything works well the following way:

<snip>

This question has been coming up a lot lately. Check groups.google.com
for a thread in this newsgroup, with the subject "URLClassLoader" and
see my reply of March 23rd, which explains the concepts involved.

I'd also advise you to check java.sun.com and/or javaworld.com for
articles on how classloaders work, particularly how they form a
tree-like hierachy, and how classes loaded by a classloader can have
access to classes loaded by their own classloader, or an ancestor of
their own classloader, but not a descendant of their classloader, nor
a classloader in a separate branch of the tree. Once you fully
understand that, using custom classloaders should be very easy for
you.
 
R

Roedy Green

Why? What am missing that is very obvious or silly?

One basic is that class1 != Class1 unless they were loaded from the
same classloader. From the JVM's point of view, they are entirely
different classes.

That may be completely irrelevant, but I toss it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top