views:

590

answers:

3

Hi,

I've got some code that calls..

x = getClass().getClassLoader();

This returns null though.

When I start the same code not from Eclipse, but the command line, it returns a classloader.

I can hack the code to do this...

if (getClass().getClassLoader() == null)
{
 x = ClassLoader.getSystemClassLoader().getSystemResourceAsStream( loadedPropFileName );
}

both are compiled and run with the same JVM. (I'm 99.99% sure).

Anyone have any ideas why the first would return null for the classloader?

Thanks Jeff Porter :-)

Edit:

My question is does "Anyone have any ideas why the same class would return null when started via Eclipse and a class loader when loaded from the command line."

Thanks for the advice that the Bootstap loader must be loading the class in Eclipse. I've no idea why this happens though.

+7  A: 

Citing the API doc:

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.

Michael Borgwardt
True, but if the implementation is the same, why the different behavior in command line and Eclipse. I think this is what the OP is really asking...
bruno conde
If he wants to know something different than what he’s asking for, why doesn’t he ask for what he really wants to know?
Bombe
+2  A: 

"This method will return null in such implementations if this class was loaded by the bootstrap class loader." http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getClassLoader()

Itay
A: 

One thing is certain, Eclipse has a deeper and more complicated classloader setup than when you are running from the command line. If you are seeing differences in how a class's classloader appears in one versus the other then that is a pretty likely reason.

I'm not knowledgeable in what exactly Eclipse is doing but I think it very likely that your class is not being loaded by the bootstrap classloader when being run from Eclipse but that Eclipse is attempting to make it seem that way.

The bootstrap ClassLoader is static once the application is bootstrapped and you can't add jars or classes to it later unless Eclipse has overridden the implementation... in which case, there's yet another possible explanation.

PSpeed