aspose file tools
The moose likes Java in General and the fly likes Problem using custom ClassLoader Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Problem using custom ClassLoader

rakesh nagar
Greenhorn

Joined: Aug 02, 2001
Posts: 17
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
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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
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
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
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
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
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
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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4761
    
    7

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?
Martin Vajsar
Bartender

Joined: Aug 22, 2010
Posts: 2335
    
    2

The same question was asked here. Please continue the discussion there.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem using custom ClassLoader
 
Similar Threads
Help! findResource isn't working!
Problem using custom ClassLoader
Casting a class to an anonymous class
ClassCastException: Loading from two Classloaders.
Class.forName() and .class not working but .getClass() works