• Post Reply 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

Getting Classloader as null when loading class using Class.forName()

 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am loading class Test2 from class Test using Class.forName().
But when I try to get the classLoader for class Test2, i get null value.
Both classes are in the same folder.


Class Test2 :


Class Test :




Can anyone please explain why I am getting classloader as null.
Should'nt it return the same classloader as that of class Test.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation of Class.getClassLoader() says:

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.


Abhineet Kapil wrote:Should'nt it return the same classloader as that of class Test.


Did you try checking what the classloader is that was used to load class Test? Maybe it's also null. Try: Test.class.getClassLoader()
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked the classloader of Test class. It is not null.

Below is the output :

ClassLoader of Test class : sun.misc.Launcher$AppClassLoader@14fe5c
ClassLoader of Test2 class : null

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have observed different behaviour when I call getClassLoader() through object reference and through Class object.



Output of above code :



So, why is there a difference when getting class loader from a object reference and that from Class name.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference you are seeing is when you are calling getClass().getClassLoader() on a Class object. This is asking for the class loader that loaded the Class object (which is presumably the bootstrap class loader) and not the class loader that loaded the test2 class.
IF you change your code to:
You will get the answer you expected.
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony...

Working perfectly fine now...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic