Hi There, I have the same problem. I have written a custom class loader. It is working fine. But the Objects which are created in the Object loaded by my custom classloader. for example i have instantiate class A using my classloader and in the Object A i am instantiate an object of class B. The problem is that the class B is loaded by default Java class loader. How can i avoid it. waiting for ur reply thanks & regards Rakesh
Use your classloader's loadClass() method prior to object instantiation. As far as I'm aware it's not possible to "earmark" a class as destined for a particular loader, except for URLClassLoaders. ------------------ Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
Steve Chernyak
Ranch Hand
Joined: Oct 19, 2000
Posts: 113
posted
0
Is class B on your system classpath, if so try removing this way the default classloader wont be able to find it. Hope this helps.
rakesh nagar
Greenhorn
Joined: Aug 02, 2001
Posts: 17
posted
0
Thanks Michael, By overriding loadClass() method in my custom classloader now it is possible to load subsequent classes using the custom classloader. Thanks again for ur reply regards Rakesh Nagar
Karthik Guru
Ranch Hand
Joined: Mar 06, 2001
Posts: 1209
posted
0
Try setting custom class loaders's parent as null super(null) in the constructor. A is loaded by custom loader. then A instantiates B. So the custom loader will get a chance to load B (since it loaded A in the first place). So as per classloader's delegation model, custom loader will first delegate the loading of class B to the parent which turns out to be null. so our loader gets a chance to load B as well. But our findClass needs to have the logic to find B. public class TestLoader extends ClassLoader{ public TestLoader(){ super(null); } public Class findClass(){ } } try this and let us know if it works. karthik.
Originally posted by rakesh nagar: Hi There, I have the same problem. I have written a custom class loader. It is working fine. But the Objects which are created in the Object loaded by my custom classloader. for example i have instantiate class A using my classloader and in the Object A i am instantiate an object of class B. The problem is that the class B is loaded by default Java class loader. How can i avoid it. waiting for ur reply thanks & regards Rakesh
Junaid Bhatra
Ranch Hand
Joined: Jun 27, 2000
Posts: 213
posted
0
Generally, with jdk 1.2 and beyond, you should be careful with overriding loadClass() method in your custom classloader, and avoid it if possible. If loadClass() is implemented incorrectly, you might interfere with the classloader delegation behavior of the JVM, and in fact it's even entirely possible to subvert the delegation behavior. Instead you should override findClass(). Also as Karthik suggested, you should have an explicit super(null) call in your classloader constructor, to avoid using the classpath classloader as your parent, which is what happens with an implict super() call. By specifying that the parent is the null classloader (or the bootstrap classloader), you are guranteed that your custom classloader will get a chance to load your user-defined class. Ofcourse this also assumes that you are NOT using the Xbootclasspath option, in which case it may also be possible for the bootstrap classloader to find your classes. [ January 08, 2002: Message edited by: Junaid Bhatra ]
rakesh nagar
Greenhorn
Joined: Aug 02, 2001
Posts: 17
posted
0
Hi All, Thanks a lot for your valuable feedbacks. It really solved my problem. regards, Rakesh Nagar
DeepakN kumar
Greenhorn
Joined: Apr 14, 2012
Posts: 27
posted
0
HI,
If we can overriding loadClass and subvert the delegation order, we would be able to even load the system packages like java*, javax* etc right? So isn't it a security threat in java?
DeepakN kumar wrote:If we can overriding loadClass and subvert the delegation order, we would be able to even load the system packages like java*, javax* etc right? So isn't it a security threat in java?
Erm...why are you reviving a 10-year old thread? I believe that must be some sort of record.
Winston
Isn't it funny how there's always time and money enough to do it WRONG?