Get the Stack trace information in Java SE 5.0

JAVA SE 5.0 provides two very useful APIs to help us debug our program, All this two are all class "Thread"’s method:

public StackTraceElement[] getStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread. This method will return a zero-length array if this thread has not started or has terminated. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

public static Map<Thread, StackTraceElement[]> getAllStackTraces()

Returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.

We can use this two APIs to get the thread’s stack trace information and help us debug or log. I will give a example that use this to write log.

If we are going to add log before, we need to add a "java.util.logging.Logger" to our class and use it to log. It’s ok and works pretty well, the following code just wanna to give you an example of the new exposed API "getStackTrace".

  1. public static void log(Level level, String message) {
  2.         Thread currentThread = Thread.currentThread();
  3.         StackTraceElement[] sts = currentThread.getStackTrace();
  4.         if (sts != null && sts.length > 3) {
  5.             StackTraceElement ste = sts[3];
  6.             String fileName = ste.getFileName();
  7.             String className = ste.getClassName();
  8.             String methodName = ste.getMethodName();
  9.             int lineNum = ste.getLineNumber();
  10.             LogRecord record = new LogRecord(level, message + "At line " + lineNum + " in file " + fileName);
  11.             record.setSourceClassName(className);
  12.             record.setThreadID((int) currentThread.getId());
  13.             record.setSourceMethodName(methodName);           
  14.             logger_.log(record);
  15.         } else {
  16.             logger_.log(level, message);
  17.         }
  18.     }

Caught the meaning? Yes, it’s pretty simple.

sts[0] = java.lang.Thread :: dumpThreads

sts[1] = java.lang.Thread :: getStackTrace

sts[2] = LogTest :: log ( This function )

sts[3] = Where you called the log function

And you can use this stack trace information to finish other features.

Popularity: 35% [?]

Related entries:

5 Responses to “Get the Stack trace information in Java SE 5.0”

  1. hotleaf Says:

    You can also get stack trace by (new Throwable()).getStackTrace(), which was presented in Java SE 1.4.

  2. Meng Yan Says:

    Yes, and the new API’s point is to get the *specific* thread’s stack trace.

  3. NovemberRain Says:

    你被点名了http://spaces.msn.com/
    members/jianxin-liang/Blog
    /cns!1ptK9M_fmx0DPL0i8DelgRKA!
    275.entry?owner=1

  4. coderman Says:

    请问您的Java代码是怎么变色贴上去的,是WP的插件吗?

  5. Meng Yan Says:

    对,搜一下CoolCode插件。

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI