How can I get the current stack trace in Java?

ripper234 picture ripper234 · Jul 1, 2009 · Viewed 541k times · Source

How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace?

I found Thread.dumpStack() but it is not what I want - I want to get the stack trace back, not print it out.

Answer

jjnguy picture jjnguy · Jul 1, 2009

You can use Thread.currentThread().getStackTrace().

That returns an array of StackTraceElements that represent the current stack trace of a program.