-1

There is a class A, which calls a methods of class B.

Now, I am generating class B dynamically, and every time when new class B is generated, A should call the newer version of class B. Now, I am loading class B using custom class loader, but class B also gets loaded using default class loader from within class A.

So, the class loaded by my custom class loader becomes irrelevant. Class B must only get loaded using custom class loader because my need is: I want to reload the class B more than once.

How to proceed?

From where I should load the class B? Is it from within class A or even before class A gets loaded? Also, is it required to load class A using custom class loader too, as Class A is going to call methods of class B?

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Why (and how) does class B get loaded from the default class loader? Can you prevent that? – Thilo Sep 02 '14 at 06:06
  • Don't spend as this time and energy coming up with a long explanation; instead, show us your code! – musical_coder Sep 02 '14 at 06:08
  • No, I dont know how to prevent it. So, now in JVM there are two class types of B, One loaded by default class loader and othe rloaded by cutom class loader. And, due to the latter part, I cant reload the class type B ,as its loaded by default class loader – sunny patel Sep 02 '14 at 06:15

1 Answers1

0

Make sure A is loaded through the same classloader that B is loaded from.

Unless you are only passing around primitives, you get ClassCastExceptions passing instances between classloaders.

triggerNZ
  • 4,221
  • 3
  • 28
  • 34