如何在 Java 中获取当前堆栈跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1069066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:04:42  来源:igfitidea点击:

How can I get the current stack trace in Java?

stack-tracejava

提问by ripper234

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

我如何在 Java 中获取当前堆栈跟踪,就像在 .NET 中一样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.

我找到了,Thread.dumpStack()但这不是我想要的 - 我想取回堆栈跟踪,而不是打印出来。

采纳答案by jjnguy

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

您可以使用Thread.currentThread().getStackTrace().

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

它返回一个StackTraceElements数组,表示程序的当前堆栈跟踪。

回答by ripper234

Silly me, it's Thread.currentThread().getStackTrace();

傻我,这是 Thread.currentThread().getStackTrace();

回答by butterchicken

try {
}
catch(Exception e) {
    StackTraceElement[] traceElements = e.getStackTrace();
    //...
}

or

或者

Thread.currentThread().getStackTrace()

回答by Adamski

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).

要获取所有线程的堆栈跟踪,您可以使用 jstack 实用程序、JConsole 或发送 kill -quit 信号(在 Posix 操作系统上)。

However, if you want to do this programmatically you could try using ThreadMXBean:

但是,如果您想以编程方式执行此操作,您可以尝试使用 ThreadMXBean:

ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);

for (ThreadInfo info : infos) {
  StackTraceElement[] elems = info.getStackTrace();
  // Print out elements, etc.
}

As mentioned, if you only want the stack trace of the current thread it's a lot easier - Just use Thread.currentThread().getStackTrace();

如前所述,如果您只想要当前线程的堆栈跟踪,那就容易多了 - 只需使用Thread.currentThread().getStackTrace();

回答by Yishai

Thread.currentThread().getStackTrace();

is fine if you don't care what the first element of the stack is.

如果您不关心堆栈的第一个元素是什么,那很好。

new Throwable().getStackTrace();

will have a defined position for your current method, if that matters.

如果这很重要,将为您当前的方法定义一个位置。

回答by RealHowTo

Thread.currentThread().getStackTrace();

is available since JDK1.5.

从 JDK1.5 开始可用。

For an older version, you can redirect exception.printStackTrace()to a StringWriter():

对于旧版本,您可以重定向exception.printStackTrace()StringWriter()

StringWriter sw = new StringWriter();
new Throwable("").printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();

回答by Leif Gruenwoldt

for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.println(ste);
}

回答by Vicky Kapadia

On android a far easier way is to use this:

在 android 上,一个更简单的方法是使用这个:

import android.util.Log;
String stackTrace = Log.getStackTraceString(exception); 

回答by Salvador Valencia

I have a utility method that returns a string with the stacktrace:

我有一个实用方法,它返回一个带有堆栈跟踪的字符串:

static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    pw.flush();
    sw.flush();
    return sw.toString();
}

And just logit like...

就像登录一样......

... 
catch (FileNotFoundException e) {
    logger.config(getStackTrace(e));
}

回答by stikkos

You can use Apache's commons for that:

您可以使用 Apache 的公共资源:

String fullStackTrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e);