How to get Java Call Stack of a running application

Have a look at Throwable.getStackTrace(). Just create a new Throwable; you don't actually have to throw it.


Method 1: Use jstack utility from command line (part of the JDK distro).

Method 2: Send signal 3 to the java process, it will dump stack traces on stdout.

Method 3: Call Thread.getAllStackTraces () from within application:

public class StackTraceDumper
{
    public static dumpAllStackTraces ()
    {
        for (Map.Entry <Thread, StackTraceElement []> entry: 
            Thread.getAllStackTraces().entrySet ())
        {
            System.out.println (entry.getKey ().getName () + ":");
            for (StackTraceElement element: entry.getValue ())
                System.out.println ("\t" + element);
        }
    }
}

Then use StackTraceDumper.dumpAllStackTraces() where you need to dump stack traces.


Thread.dumpStack() Prints a stack trace of the current thread to the standard error stream. Thread.getAllStackTraces() Returns a map of stack traces for all live threads. Thread.getStackTrace() Returns an array of stack trace elements representing the stack dump of this thread.