Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem using custom ClassLoader

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All,
Thanks a lot for your valuable feedbacks. It really solved my problem.
regards,
Rakesh Nagar
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The same question was asked here. Please continue the discussion there.
    Bookmark Topic Watch Topic
  • New Topic