java: printing current backtrace

ufk picture ufk · Feb 22, 2011 · Viewed 8.4k times · Source

is there a way to add a command in Java to add the current backtrace ?

I'm writing a red5 application and the appDisconnect function is being called twice. whenever a user changes room. I want to add a function at the beginning of the appDisconnect function that shows the current backtrace and then I can see what called it.

thanks

Answer

Jörn Horstmann picture Jörn Horstmann · Feb 22, 2011

You can output the stack trace to the current line like this:

new Exception().printStackTrace();

Or if you need programmattic acces the the stacktrace elements you can use

Thread.currentThread().getStackTrace()