如何获取 Java 中当前的堆栈跟踪?
如何在 Java 中获取当前的堆栈跟踪,就像在 .NET 中一样Environment.StackTrace
?
我找到了 Thread.dumpStack() ,但这不是我想要的 - 我想取回堆栈跟踪,而不是将其打印出来。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
您可以使用
Thread.currentThread ().getStackTrace()
。返回一个
StackTraceElement
的数组 代表程序的当前堆栈跟踪。You can use
Thread.currentThread().getStackTrace()
.That returns an array of
StackTraceElement
s that represent the current stack trace of a program.如果您不关心堆栈的第一个元素是什么,那就没问题。
如果重要的话,将为您当前的方法指定一个明确的位置。
is fine if you don't care what the first element of the stack is.
will have a defined position for your current method, if that matters.
托尼,作为对已接受答案的评论,给出了似乎是最佳答案,实际上回答了OP的问题:
......OP确实没有 询问如何从
Exception
的堆栈跟踪中获取String
。 尽管我是 Apache Commons 的超级粉丝,但当有像上面这样简单的东西时,就没有逻辑理由使用外部库。Tony, as a comment to the accepted answer, has given what seems to be the best answer which actually answers the OP's question:
... the OP did NOT ask how to get a
String
from the stack trace from anException
. And although I'm a huge fan of Apache Commons, when there is something as simple as the above there is no logical reason to use an outside library.从 JDK1.5 开始可用。
对于旧版本,您可以将
exception.printStackTrace()
重定向到StringWriter()
:is available since JDK1.5.
For an older version, you can redirect
exception.printStackTrace()
to aStringWriter()
:您可以使用 Apache 的 commons 来实现:
You can use Apache's commons for that:
在 Android 上,一个更简单的方法是使用这个:
On android a far easier way is to use this:
另一种解决方案(只有
3531 个字符):Another solution (only
3531 characters):要获取所有线程的堆栈跟踪,您可以使用 jstack 实用程序、JConsole 或发送kill -quit 信号(在 Posix 操作系统上)。
但是,如果您想以编程方式执行此操作,您可以尝试使用 ThreadMXBean:
如前所述,如果您只需要当前线程的堆栈跟踪,那就容易得多 - 只需使用 Thread.currentThread().getStackTrace()代码>;
To get the stack trace of all threads you can either use the jstack utility, JConsole or send a kill -quit signal (on a Posix operating system).
However, if you want to do this programmatically you could try using ThreadMXBean:
As mentioned, if you only want the stack trace of the current thread it's a lot easier - Just use
Thread.currentThread().getStackTrace()
;Java 9 中有一种新方法:
In Java 9 there is a new way:
我建议这
是一种更简单的方法,并且具有在可能根本不存在问题时不实际构造异常或可抛出的优点,并且更切题。
I suggest that
is an easier way and has the advantage of not actually constructing an exception or throwable when there may not be a problem at all, and is considerably more to the point.
愚蠢的我,它是 Thread.currentThread().getStackTrace();
Silly me, it's
Thread.currentThread().getStackTrace();
获取堆栈跟踪:
打印堆栈跟踪(JAVA 8+):
打印堆栈跟踪(JAVA 7):
Getting stacktrace:
Printing stacktrace (JAVA 8+):
Printing stacktrage (JAVA 7):
用番石榴串起来:
To string with guava:
我有一个实用程序方法,它返回带有堆栈跟踪的字符串:
并且只需像这样登录...
I have a utility method that returns a string with the stacktrace:
And just logit like...
或者
or
也许你可以尝试这个:
字符串“errorDetail”包含堆栈跟踪。
Maybe you could try this:
The string 'errorDetail' contains the stacktrace.
循环 StackTraceElement 并获得您想要的结果。
loop through StackTraceElement and get your desired result.
如果您想检查进程的当前调用堆栈,可以使用 jstack 实用程序。
You can use jstack utility if you want to check the current call stack of your process.
我使用了上面的答案并添加了格式
I used answers from above and added formatting
对于那些只想将当前堆栈跟踪记录到日志中的人来说,我会选择:
干杯
For people, who just want to get the current stacktrace to their logs, I would go with:
Cheers
您还可以使用异常来打印堆栈,而不用费力地放置新行字符:
You can also use exception to print stack instead of taking pain of putting new line char:
这是一篇旧帖子,但这是我的解决方案:
更多信息和更多方法:
http://javarevisited .blogspot.fr/2013/04/how-to-get-current-stack-trace-in-java-thread.html
This is an old post, but here is my solution :
More info and more methods there :
http://javarevisited.blogspot.fr/2013/04/how-to-get-current-stack-trace-in-java-thread.html