File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Context Class Loader - very very confusing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Context Class Loader - very very confusing" Watch "Context Class Loader - very very confusing" New topic

Context Class Loader - very very confusing

pooja jain
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
could anyone please explain me getContextClassLoader() method and 'context class loader' in very simple way (if possible please give an example).


Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

This article may help you.
First few paragraphs answers your question.
Additionally, this is what javadocs for the method says:
The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources.

This can be different from the class loader of the current class(aka current classloader), a method of which is being executed.

apigee, a better way to API!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The Thread class provides the concept of a "context class loader". This is a ClassLoader associated with that Thread. Any thread started by that Thread will inherit the same context class loader. The initial main thread has the system class loader as its context class loader, but this can be changed via setContextClassLoader().

The context class loader will only be used to load classes if you specifically request it. By default, when JVM encounters a class that hasn't yet been loaded, it loads it with the ClassLoader that loaded the class that's currently running in that thread. To use the context class loader, you have to call loadClass() on the ClassLoader object returned by getContextClassLoader().

Any help?

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Context Class Loader - very very confusing
It's not a secret anymore!