Tom:
No, I haven't assumed anything of the sort. When one class directly
references another in code, the JVM loads both classes with the SAME
classloader. It does not use Thread.currentThread().getContextClassLoader
to load the second class. Now, you can do dynamic classloading by calling
loadClass explicitly, but you cant then explicitly reference that class name
in your code the way Ivanhoe is doing. Its not hard to test it out:
I made 4 classes:
Class A directly references Class B in its constructor:
class A{
A() {
System.out.println("context classloader is:
"+Thread.currentThread().getContextClassLoader());
B();
}
}
A is available at URL1
B is available at URL2
class C is a classloader (extends URLClassLoader) that prints out debugging
info when it loads a class.
class D has main():
main {
try {
C cl1 = new C("cl1", {URL1, URL2}); //cl1 can see A and B
C cl2 = new C("cl2", {URL1, URL2}); //cl2 can see A and B
C cl3 = new C("cl3", {URL1}); //cl3 can only see A
Thread.currentThread().setContextClassLoader(cl1);
c1.loadClass("A").newInstance();
c2.loadClass("A").newInstance();
c3.loadClass("A").newInstance();
}
...
}
output is:
cl1 loading A
context classloader is: cl1
cl1 loading B
cl2 loading A
context classloader is: cl1
cl2 loading B
cl3 loading A
context classloader is: cl1
cl3 loading B
java.lang.NoClassDefFoundError: B
at A.<init>(A.java:4)
at java.lang.Class.newInstance0(Native Method)
.......
So even though A's context class loader is always c1, B is always loaded by
A's DEFINING class loader.
-----Original Message-----
From: Tom Cook [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 14, 2001 4:39 PM
To: jBoss
Subject: RE: [jBoss-User] Dynamic classloading
On Wed, 14 Feb 2001, you wrote:
> Forgive me if Im missing the obvious. Im new to JNDI, but I just dont see
> how your code could work with nothing in the classpath.
>
> You are directly referencing the SKILLSHome class in your code.
Therefore,
> the class loader that is trying to load the SKILLSHome is the system
> classloader (since I see Dynamictest.main in your stack trace). And youve
> just said that SKILLSHome is NOT in the system classpath. So you will
> always get NoClassDefFoundError!
No.
> I think you must either: 1. have SKILLSHome in your classpath or 2. have
> SKILLSHome implement an interface that is in your classpath or 3. Cast
your
> PRO.Narrow to an Object and use reflection to call methods on it. or 4.
> reference SKILLSHome directly from a second class which is dynamically
> loaded by a classloader that can see SKILLSHome.
You have assumed that it is not possible to chage the context classloader
for a
thread. It is possible to create a new class loader which will download the
classes from an HTTP server; see my other posts for how. Then, when you
have
installed this new classloader, you don't need the classes on your system
classpath, because they will be loaded by a child classloader (ie. the one
you
have just created).
Tom
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
List Help?: [EMAIL PROTECTED]
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
List Help?: [EMAIL PROTECTED]