Reputation: 20976
What is easiest way to convert java.lang.Thread.getStackTrace()
to string?
There are plenty of methods how to convert Throwable
to string, but I need to get stack trace of particular thread object.
Upvotes: 0
Views: 436
Reputation: 1
Apache Commons provides exception utilities to assist with this kind of issue. Among these utilities is a method which accepts a Throwable object and returns a String. You should be able to create a Throwable
object on your thread, passing it to this function, and using the return as needed.
The method I am referring to can be found here: Apache Commons Exception Utilities - getStackTrace
Upvotes: 0
Reputation: 533790
You can do this
StringBuilder sb = new StringBuilder();
for(StackTraceElement ste: Thread.getStackTrace())
sb.append("\tat ").append(ste).append("\n");
String trace = sb.toString();
Upvotes: 3