Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Back to your Class roots

Find a class's runtime origin

  • Print
  • Feedback

Page 2 of 2

Should the first step fail, try locating the class definition as a classloader resource with a .class extension. Java specifications do not detail whether this is allowed: arbitrary code with the ability to read entire class definitions via URLs is a potential security hole, and some JVMs forbid getResource() for *.class resources. However, Sun Microsystems' JDK implementation uses this approach for normal classloading, and that seems to convey some measure of legality on it.

Finally, don't forget that you can't find something that doesn't exist: a java.lang.Class object in a JVM does not need to be backed by a real .class file anywhere. An obvious example here is a dynamic proxy class: its byte-code definition is synthesized at runtime, and getClassLocation() returns null for such a class. Most likely, future J2SE versions will rely even more on runtime class construction. For that and classloader-related reasons, you should use the method I've shown only for testing and debugging.

About the author

Vladimir Roubtsov has programmed in a variety of languages for more than 13 years, including Java since 1995. Currently, he develops enterprise software as a senior engineer for Trilogy in Austin, Texas.
  • Print
  • Feedback

Resources