user917879
user917879

Reputation: 137

In where scenario we will use Thread.getContextClassLoader() and ClassLoader.getSystemClassLoader() can instand of it?

After go through google i indeed could not find an example for how to use Thread.getContextClassLoader(),

maybe this question is duplicate as below Difference between thread's context class loader and normal classloader

My question is recap below: As i know a scenario for using Thread.getContextLoader, Becasue JNDI core classes included in /lib/rt.jar but those JNDI Core (maybe they are only some interface) have not bean implemented, in other works if you want to use JNDI's functions you must provide a JNDI's implementation, an then we place those implementation (maybe are jars) into system classpath, but now the core JDNI classes are loaded by Bootstrap and those core classes have to use its implementation classes, Ok we supply an Thread.getContextClassLoader() (if you no have any operations default is ClassLoader.getSystemClassLoader()) for him, it can load those now, i just supposed that why the core JNDI classes using this manner to get the systemClassLoader 'ClassLoader.getSystemClassLoader()' directly?

Maybe some points i explained not correct..

<1> but i just wanna to understand if we can use ClassLoader.getSystemClassLoader() instand of Thread.getClassLoader() to loader System classpath classes or resouces in the classes which loaded by bootstrap classloader?

Upvotes: 3

Views: 3054

Answers (1)

AlexR
AlexR

Reputation: 115378

Thread.getContextClassLoader() was introduced to java not from the beginning. It is very useful in multi-classloader environments, shortly for Java EE applications running in container that typically allocates private class loader for each enterprise application. So, resources packaged with this application can be retrieved using context class loader.

It is used also for self configuration of logging systems like commons logging and log4j.

Upvotes: 1

Related Questions