Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get the caller's class loader

807607Dec 5 2006 — edited Dec 6 2006
I'm pretty new to java class loading and its mechanisms. I wonder if is possible to get the caller's class loader mentioned in Class#getClassLoader's JavaDoc as i get a security exception in conjunction with it:

If a security manager is present, and the caller's class loader is not null and the caller's class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the class loader for the class.

I got an application with a security manager enabled. Let's assume the statement

myObj.getClass().getClassLoader();

the caller's class loader then is the class loader that loaded the code for getClass(), right? How can I find out, what class loader did that?

I can write

System.out.println(myObj.getClass().getClassLoader());

to get information about the requested class loader, but I wonder how it is set to be the requested one.

I'm working in a tomcat enviroment. Let's assume that webappCL is the class loader for all classes located in WEB-INF/lib and WEB-INF/classes, so if the getClass() call above returns a class located in WEB-INF/classes for example, then webappCL is the requested class loader?

Let's assume I define my own class loader myCL. I load the class of myObj with myCL unsing loadClass(). Is myCL the caller's class loader in that case?

Regards,
Matthias
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2007
Added on Dec 5 2006
5 comments
469 views