I have read somewhere that if class A is loaded by ClassLoaderA then all the classes A depends on will be loaded by ClassLoaderA. Is it true? If it is true, then why we need to set the context class loader? like this one: http://blog.markturansky.com/archives/21, thanks in advance.
I have read somewhere that if class A is loaded by ClassLoaderA then all
Share
The Javadocs say the following about the class loaders:
As such, the statement in your question is apparently wrong.
There are cases in which you might like to create an isolated class loader, that is, a class loader not tied to the application class loader chain. In those cases you could create a class loader whose parent is null. (i.e. check constructors in URLClassLoader class)
This means that if this isolated class loader cannot find/load a given class it will immediately fail with a
ClassNotFoundException. This means also that you can have two independent hierarchies of class loaders not interfering with each other.-EDIT-
I am going to illustrate my answer. Assume that:
Creaturelocated in a JAR file calledparent.jar.Jedilocated in a JAR file calledchild.jar.Jediimplements the interfaceCreature.Now, let’s create a chain of class loaders, independent of the current system chain.
Now, we can ask the child class loader to load the class
Jedi.Now, if you query its class loader, you will see that it was loaded by the child class loader. So far so good. But what about the interface it implements. It depends on it, but the
Creatureinterface was only available to its parent class loader, right?Well, if you request the
klass1.getInterfaces()[0].getClassLoader()you will notice that it is the parent class loader.This proves that the statement:
is totally false.