*
The moose likes Java in General and the fly likes Getting Classloader as null when loading class using Class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting Classloader as null when loading class using Class.forName()" Watch "Getting Classloader as null when loading class using Class.forName()" New topic
Author

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

Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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()

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

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

Joined: Feb 08, 2010
Posts: 52

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.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2169
    
  47
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

Joined: Feb 08, 2010
Posts: 52

Thanks Tony...

Working perfectly fine now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Classloader as null when loading class using Class.forName()
 
Similar Threads
Dynamic class loading and lazy loading
Loading same class by two different class loader (ClassCastException)
What is the Prototype for Main method in java
Class loader
How to set a new Custom ClassLoader?