Seng Zhe
Seng Zhe

Reputation: 623

getClassloader failure reasons?

Just curious that when will the below line fail?

this.getClass().getClassLoader();

ClassLoader will ALWAYS be found right ? is there any situation that the ClassLoader cannot be found ?

Upvotes: 0

Views: 357

Answers (1)

bresai
bresai

Reputation: 377

Take a look at the source code. This method throws only SecurityException when there is a SecurityManager and you are not permitted to access it.

public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
        }
        return cl;
    }

And in case you are wondering what cl could be null. Here is an answer.

Upvotes: 1

Related Questions