R
Rainer Schwarze
Hi,
I am working on a plugin implementation (yes, another one
)
Lets assume I have a few classes in a few jars:
class X in x.jar (with main method)
class A in x.jar (together with X)
class B in b.jar
class C in c.jar
class ASub (extends A) in asub.jar
I run X.main in the x.jar.
In X a URLClassLoader is created with urls to b.jar, c.jar and asub.jar.
The classes A and C have a method (say touchB) which creates an instance
of B and calls a method from B. (It is doing that by something like: B b
= new B(); b.method(); )
Well, the following happens:
- I can create instances of the classes A, B, C, and ASub in X - I use
the reflection API for that.
- "C.touchB" can find class B
- "A.touchB" cannot find class B (gives an InvocationTargetException)
- "ASub.touchB" cannot find class B (gives an InvocationTargetException)
(I put the C.touchB and the others in quotes, because I do it with the
reflection API and not "directly".)
Looking at the ClassLoaders of B, C, and ASub reveals, that they use the
URLClassLoader. Class A has the system class loader.
Now what I do not fully understand is:
Why does ASub cannot access B?
I thought, that a class has a class loader associated and uses that to
access a class. ASub has URLClassLoader and by that can access b.jar
where the class B is located. However, class A was created by the system
class loader and thus cannot see class B. To me that means, that if I
put a subclassed class into a plugin which is loaded by a custom class
loader and the super class lies in the system class loaders scope, I
will get problems.
Do I see the situation correctly?
Does someone has hints about how to solve such a problem "the right way"?
(The basic problem I have is: Has anyone succeeded in "dynamically
loading" PLaF's by "including" jars (for instance with URLClassLoader)
from a subdirectory which are not in the original classpath for the
application?)
Best wishes,
Rainer
I am working on a plugin implementation (yes, another one
Lets assume I have a few classes in a few jars:
class X in x.jar (with main method)
class A in x.jar (together with X)
class B in b.jar
class C in c.jar
class ASub (extends A) in asub.jar
I run X.main in the x.jar.
In X a URLClassLoader is created with urls to b.jar, c.jar and asub.jar.
The classes A and C have a method (say touchB) which creates an instance
of B and calls a method from B. (It is doing that by something like: B b
= new B(); b.method(); )
Well, the following happens:
- I can create instances of the classes A, B, C, and ASub in X - I use
the reflection API for that.
- "C.touchB" can find class B
- "A.touchB" cannot find class B (gives an InvocationTargetException)
- "ASub.touchB" cannot find class B (gives an InvocationTargetException)
(I put the C.touchB and the others in quotes, because I do it with the
reflection API and not "directly".)
Looking at the ClassLoaders of B, C, and ASub reveals, that they use the
URLClassLoader. Class A has the system class loader.
Now what I do not fully understand is:
Why does ASub cannot access B?
I thought, that a class has a class loader associated and uses that to
access a class. ASub has URLClassLoader and by that can access b.jar
where the class B is located. However, class A was created by the system
class loader and thus cannot see class B. To me that means, that if I
put a subclassed class into a plugin which is loaded by a custom class
loader and the super class lies in the system class loaders scope, I
will get problems.
Do I see the situation correctly?
Does someone has hints about how to solve such a problem "the right way"?
(The basic problem I have is: Has anyone succeeded in "dynamically
loading" PLaF's by "including" jars (for instance with URLClassLoader)
from a subdirectory which are not in the original classpath for the
application?)
Best wishes,
Rainer