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

Java Tip 124: Trace your steps in Java 1.4

Locate runtime code with getStackTrace()

  • Print
  • Feedback
I don't know about you, but I really like to know where I am. Being a guy, I'm never lost, but sometimes I just don't know where I am. Some places, such as malls, have maps with "You Are Here" indicators. Similarly, Java now lets us figure out our location with the system's help. In this tip, I'll show you how to extract this location information from the system in a consistent and reliable manner.

I firmly believe the runtime system should provide enough metadata about the system itself so programs can make better decisions and complete tasks. Java has been able to introspect and reflect on classes for some time, but until now it has lacked the simple ability to map runtime code back to its position in the source code file. The pre-Java 1.4 solution was to manually parse an exception's stack trace. Now, with Java 1.4, we have a better solution.

Pull apart a stack trace?

The pre-Java 1.4 workaround to gathering location information was to manually parse an exception's printStackTrace() output. Here's an example of a simple stack trace:

java.lang.Throwable
        at boo.hoo.StackTrace.bar(StackTrace.java:223)
        at boo.hoo.StackTrace.foo(StackTrace.java:218)
        at boo.hoo.StackTrace.main(StackTrace.java:54)


Pulling the above code apart is not a major parsing problem. But how about this?

java.lang.Throwable
        at
boo.hoo.StackTrace$FirstNested$SecondNested.<init>(StackTrace.java:267)
        at boo.hoo.StackTrace$FirstNested.<init>(StackTrace.java:256)
        at boo.hoo.StackTrace.<init>(StackTrace.java:246)
        at boo.hoo.StackTrace.main(StackTrace.java:70)


Ugh. What does all that weird goobley-guk really mean, and why on earth should I have to parse it? Obviously, the system already tracks that location information, since it's able to build those stack traces. So why isn't that location information available directly? Well, with Java 1.4, it finally is.

In addition, keep in mind that in the face of JIT (just-in-time) compilers and dynamic, optimizing compilers like Sun Microsystems' HotSpot, file and line number information may not exist. The goal of "performance or bust" can certainly be bothersome.

Java 1.4 Throwable to the rescue!

After tolerating years of complaints, Sun Microsystems has finally extended the java.lang.Throwable class with the getStackTrace() method. getStackTrace() returns an array of StackTraceElements, where each StackTraceElement object provides the means to more or less directly extract location information.

To acquire that mapping information, you still create a Throwable instance at the point of interest in your code:

    //...
    public static void main (String[] args)
        {
        Throwable ex = new Throwable();
        //...


This code positions that point at the start of main().

Of course, it's useless to just gather that information without doing something with it. For this tip, we will use each underlying method of the StackTraceElements to extract and display all the information we can.

The example program, StackTrace.java, shows how to extract the location information with several examples. You will need the J2SE (Java 2 Platform, Standard Edition) 1.4 SDK to compile and run the example program.

  • Print
  • Feedback

Resources