URLClassLoader problem with inner classes

A

axel

Hello,

I have an abstract base class A with some function in a standard
package. And then I have a class B extends A with a lot of inner
classes. B is loaded by the URLClassLoader and also initialized with a
static initialize function cause B is designed as a singleton. In the
(private) ctor I create from every inner class 1 object and store it in
a hashmap.
The problem comes later if I want to access a function of A from an
inner class of B and I get this error:
"e"= IllegalAccessError (id=105)
cause= IllegalAccessError (id=105)
detailMessage= "tried to access method cmdpkg.A.foo()S from
class cmdpkg.B$_B1"
stackTrace= null

Where is the mistake. Is it a problem if the base class A is known
before and also in the jar for class B?

Thanks, Axel
 
T

Thomas Hawtin

axel said:
I have an abstract base class A with some function in a standard
package. And then I have a class B extends A with a lot of inner
classes. B is loaded by the URLClassLoader and also initialized with a

At a guess, your problem is that classes loaded by different class
loaders are effectively in different packages.

Tom Hawtin
 
C

Chris Uppal

axel said:
I have an abstract base class A with some function in a standard
package. And then I have a class B extends A with a lot of inner
classes. B is loaded by the URLClassLoader and also initialized with a
static initialize function cause B is designed as a singleton. In the
(private) ctor I create from every inner class 1 object and store it in
a hashmap.
The problem comes later if I want to access a function of A from an
inner class of B and I get this error:
"e"= IllegalAccessError (id=105)
cause= IllegalAccessError (id=105)
detailMessage= "tried to access method cmdpkg.A.foo()S from
class cmdpkg.B$_B1"
stackTrace= null

I'm a bit confused about what you are doing here, so this may be hogwash....

If you have attempted to put A and B (plus its nested classes) into the same
package, but have loaded the classes through different classloaders, then they
are /not/ in the same package (just two packages with the same name). So if
your B (or its nested classes) refers to members of A which are not public or
(in some cases) protected, then it'll fail. It'll fail at runtime rather than
compile time because javac doesn't realise that you are going to use different
classloaders.

-- chris
 
B

Ben Kraufmann

Hello,

I have an abstract base class A with some function in a standard
package. And then I have a class B extends A with a lot of inner
classes. B is loaded by the URLClassLoader and also initialized with a
static initialize function cause B is designed as a singleton. In the
(private) ctor I create from every inner class 1 object and store it in
a hashmap.
The problem comes later if I want to access a function of A from an
inner class of B and I get this error:
"e"= IllegalAccessError (id=105)
cause= IllegalAccessError (id=105)
detailMessage= "tried to access method cmdpkg.A.foo()S from
class cmdpkg.B$_B1"
stackTrace= null

Where is the mistake. Is it a problem if the base class A is known
before and also in the jar for class B?

Thanks, Axel

Hi.

Is A.foo() a public function or is it package?
If it is package, the problem is, that B and A are not in the same
package! You are loading B with your own ClassLoader. So the full
qualified class name is lets say myCL.cmdpkg.B. However, A is
appLoader.cmdpkg.A, where appLoader is the default application class
loader. This means, that your local A and your remote B are not in the
same package and A's foo() must have public visibility to be called from
within your remote B.

Ben
 
A

axel

Thank you all for your answers. And all answers have the same content:
The function A.foo() is protected and I have to make it public.
But I don't know the following (but I will try it in the next minutes):
I have also defined an interface Ibase and I have a class where I
register objects of THIS interface. But was is if B is implementing
this interface which comes from my class loader and the register class
want interfaces from the standard class loader? Is my way thinking
about this is wrong?

Thanks, Axel
 
C

Chris Uppal

axel said:
But I don't know the following (but I will try it in the next minutes):
I have also defined an interface Ibase and I have a class where I
register objects of THIS interface. But was is if B is implementing
this interface which comes from my class loader and the register class
want interfaces from the standard class loader? Is my way thinking
about this is wrong?

Sorry, but I can't really understand that paragraph.

Anyway, as far as I can tell you have a scheme where there's an interface which
the classes loaded via your classloader must implement, and which is known and
used by a class which is not loaded via your classloader. If so then that's a
pretty standard arrangement, and should work OK. The thing to do is to ensure
that the interface is loaded from outside your custom classloader, and that
that classloader's parent (or possibly grandparent or other ancestor) is the
one that loaded the interface.

-- chris
 
A

axel

All works fine and it works like you have descirbed. But my question
was, if the interface comes also from the class loader and there exist
one from the standard class loader, then exist 2 interfaces from this
moment. But I have seen in the meantime that the interface is not
loaded abain by the URL class loader.

axel
 

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,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top