views:

1134

answers:

2

In Java How could getClass().getClassLoader() return null?

  • The jar the 'class' is located in is NOT located under common/lib.
  • The jar is NOT being boostrap-loaded.

The ClassLoader for all classes within the jar is null.

+4  A: 

From the documentation...

public ClassLoader getClassLoader()

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.

...

If this object represents a primitive type or void, null is returned.

Adam Wright
You would need to be running as a bootstrap class (e.g. using -Xbootclasspath/a:
Tom Hawtin - tackline
Thanks, I knew I was probably missing something mundane and over complicating the problem. Let's hope this leads to a solution to my REAL problem.
Randyaa
I'm trying to help another developer that's experiencing this issue. I was hoping this would solve it, but unfortunately it doesn't. They're not bootstrap-loading anything. Any other suggestions would be helpful.They're using Spring to load the class which is CALLING the code where the getClass().getClassLoader() returns null
Randyaa
+1  A: 

Are you using any endorsed standards? I believe this would cause the classes to be loaded by the bootstrap class loader even though you may not be explicitly using -Xbootclasspath.

Andrew