X

Sundararajan's Weblog

  • Java
    September 1, 2004

Java stack traces

Guest Author
From the initial version, Java printed stack trace for unhandled exception or error. This helps a lot in debugging/troubleshooting. What if you want stack trace at arbitrary point in your program? Well, you could use

try {
throw new RuntimeException();
} catch (RuntimeException re) {
re.printStackTrace();
}

Or even simply (new Throwable()).printStackTrace() as Michael Stevens pointed out (refer to "comments"). You don't need to throw and catch as the constructor initializes stack trace.

J2SE 1.4 instroduced Throwable.getStackTrace() API. Using this API, you can programmatically access stack frames from the exception.

If we don't change our Java program to get stack traces, we can send SIGQUIT (Ctrl-\\ Unix platforms) or press Ctrl-Break on Windows. This prints stack traces for all Java threads at the console. This is very useful because you can get stack traces at any point rather than instrumenting your Java program. But, if JVM is badly hung or crashed and you only have a core dump, this feature will not help you! Also, if you start the JVM with -Xrs (reduce signal handler usage) command line option, then this feature is disabled. Or, if there is no console for your Java program (may be you are running javaw.exe on Windows), you can't get the stack traces.

With J2SE 1.5, there are lot of improvements in the area of stack traces.

  • J2SE 1.5 introduces new API java.lang.Thread.getStackTrace()
    to get stack trace of current thread at any point in your Java program (there is also Thread.getAllStackTraces() method to get stack traces of all Java threads).

  • With J2SE 1.5, you can use the new jstack command line utility to get Java stack traces from live process (may be hung even) or core dump.

  • When JVM crashes, it writes a fatal error log file (named "hs_err_pid<pid>.log") under the current working directory. With J2SE 1.5, fatal error log now includes Java stack trace (as well as C/C++ symbols) of the "faulting" thread.

  • If you running J2SE 1.5 on Solaris 10, you may also use Solaris "pstack" to get stack trace -- pstack has been enhanced to print Java method names in stack trace. Solaris dtrace includes ustack action that prints native symbols and Java method names in stack trace.

Join the discussion

Comments ( 4 )
  • Michael Stevens Friday, September 3, 2004
    I've always used (new Throwable()).printStackTrace();
    You don't actually need to throw the exception.
  • guest Friday, September 3, 2004
    Yes, Michael Stevens is right. We don't need to actually throw the exception and catch it. Constructor of throwable collects the stack trace.
  • Santosh Nair Wednesday, October 6, 2004
    Hello Sundarajan
    We need some assistance in identifying freeze issue with one of java components of our product.Current underlying JRE is IBM 1.3.0,
    We would like to shift to J2SE 1.5.Could we get help from SUN ?
    my contact no is 9844068340.id is santoshkn@elind.com
    Thanks
    Santosh Nair
  • Sundararajan Thursday, December 2, 2004
    Hello Santhosh, sorry about answering late (I wasn't expecting a comment from this topic). There is a JDK 1.5 Troubleshooting guide at http://java.sun.com/j2se/1.5/pdf/jdk50_ts_guide.pdf. In addition to describing tools/techniques, this document also includes information about support options available from Sun.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.