Determining from Where a Class Was Loaded

// Get the location of this class Class cls = this.getClass(); ProtectionDomain pDomain = cls.getProtectionDomain(); CodeSource cSource = pDomain.getCodeSource(); URL loc = cSource.getLocation(); // file:/c:/almanac14/examples/
It is not possible to determine the location of classes loaded by the system class loader in the same way since the class' code source is null. The only other method is to use the -verbose option on the java command. This causes the Java virtual machine to print a message every time a class is loaded.
> java -verbose MyApp
Here's a sample of the output:
[Opened c:\jdk1.4\jre\lib\rt.jar] [Opened c:\jdk1.4\jre\lib\sunrsasign.jar] [Opened c:\jdk1.4\jre\lib\jsse.jar] [Opened c:\jdk1.4\jre\lib\jce.jar] [Opened c:\jdk1.4\jre\lib\charsets.jar] [Loaded java.lang.Object from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.io.Serializable from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.Comparable from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.CharSequence from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.String from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.Class from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.Cloneable from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.ClassLoader from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.System from c:\jdk1.4\jre\lib\rt.jar] [Loaded java.lang.Throwable from c:\jdk1.4\jre\lib\rt.jar]

Comments

5 May 2010 - 1:01am by Pratibh (not verified)

Very Nice article. I juste wanted to know, in case of multi threaded application, where every thread is responsible for creating and destroying Object of Class A.

If the threads are running one after another with no overlapping period and may have some deplay in there execution, how many times, Class A will be loaded.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.