Jak Mogę uzyskać bieżący ślad stosu w Javie?
Jak uzyskać bieżący stos trace w Javie, jak w. NET można zrobić Environment.StackTrace
?
Znalazłem Thread.dumpStack()
ale to nie jest to, czego chcę - chcę odzyskać ślad stosu, a nie wydrukować go.
22 answers
Możesz użyć Thread.currentThread().getStackTrace()
.
Który zwraca tablicę StackTraceElement
s, które reprezentują bieżący ślad stosu programu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-12 14:15:42
Thread.currentThread().getStackTrace();
Jest w porządku, jeśli nie obchodzi cię, jaki jest pierwszy element stosu.
new Throwable().getStackTrace();
Będzie miał określoną pozycję dla bieżącej metody, jeśli to ma znaczenie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-01 13:29:12
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-04 06:46:24
Thread.currentThread().getStackTrace();
Jest dostępny od JDK1. 5.
Dla starszej wersji można przekierować {[2] } na StringWriter()
:
StringWriter sw = new StringWriter();
new Throwable("").printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-20 15:32:45
Możesz do tego użyć commons Apache:
String fullStackTrace = org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-03 19:43:43
Tony, jako komentarz do zaakceptowanej odpowiedzi, dał to, co wydaje się być najlepszą odpowiedzią, która faktycznie odpowiada na pytanie OP :
Arrays.toString(Thread.currentThread().getStackTrace()).replace( ',', '\n' );
... the OP did NIE zapytaj jak uzyskać {[1] } ze stosu trace z Exception
. I chociaż jestem wielkim fanem Apache Commons, kiedy jest coś tak prostego jak wyżej, nie ma logicznego powodu, aby używać zewnętrznej biblioteki.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-02 08:04:00
Na Androidzie o wiele łatwiej jest użyć tego:
import android.util.Log;
String stackTrace = Log.getStackTraceString(exception);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-26 22:25:07
Aby uzyskać ślad stosu wszystkich wątków, możesz użyć narzędzia jstack, JConsole lub wysłać sygnał kill-quit (w systemie operacyjnym Posix).
Jednak, jeśli chcesz to zrobić programowo, możesz spróbować użyć ThreadMXBean:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);
for (ThreadInfo info : infos) {
StackTraceElement[] elems = info.getStackTrace();
// Print out elements, etc.
}
Jak już wspomniano, jeśli chcesz tylko śledzić bieżący wątek, jest o wiele łatwiej-po prostu użyj Thread.currentThread().getStackTrace()
;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-20 14:43:45
Inne rozwiązanie (tylko 35 31 znaki):
new Exception().printStackTrace();
new Error().printStackTrace();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-18 06:57:28
Głupi ja, to jest Thread.currentThread().getStackTrace();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-01 13:31:33
Zdobywanie stacktrace:
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
Printing stacktrace (JAVA 8+):
Arrays.asList(ste).forEach(System.out::println);
Printing stacktrage (JAVA 7):
StringBuilder sb = new StringBuilder();
for (StackTraceElement st : ste) {
sb.append(st.toString() + System.lineSeparator());
}
System.out.println(sb);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-17 12:19:08
Proponuję
Thread.dumpStack()
Jest łatwiejszym sposobem i ma tę zaletę, że nie konstruuje wyjątków lub wyrzuca, gdy nie ma problemu w ogóle, i jest znacznie bardziej do rzeczy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-18 01:55:40
W Javie 9 jest nowy sposób:
public static void showTrace() {
List<StackFrame> frames =
StackWalker.getInstance( Option.RETAIN_CLASS_REFERENCE )
.walk( stream -> stream.collect( Collectors.toList() ) );
for ( StackFrame stackFrame : frames )
System.out.println( stackFrame );
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-28 20:47:44
Mam metodę utility, która zwraca łańcuch ze stosem:
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();
}
I po prostu logit jak...
...
catch (FileNotFoundException e) {
logger.config(getStackTrace(e));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-06 23:54:10
Do napisu z guawy:
Throwables.getStackTraceAsString(new Throwable())
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-24 12:45:26
try {
}
catch(Exception e) {
StackTraceElement[] traceElements = e.getStackTrace();
//...
}
Lub
Thread.currentThread().getStackTrace()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-07-01 13:19:06
Może mógłbyś spróbować tego:
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
e.printStackTrace(pw);
String errorDetail = writer.toString();
}
Łańcuch "errorDetail" zawiera stacktrace.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-20 15:11:59
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
Ostatni element tablicy przedstawia dno stosu, które jest najmniej aktualnym wywołaniem metody w sekwencji.
A StackTraceElement has getClassName(), getFileName(), getLineNumber() and getMethodName().
Pętla przez StackTraceElement i uzyskać pożądany wynik.
for (StackTraceElement ste : stackTraceElements )
{
//do your stuff here...
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-23 08:09:07
Użyłem odpowiedzi z góry i dodałem formatowanie
public final class DebugUtil {
private static final String SEPARATOR = "\n";
private DebugUtil() {
}
public static String formatStackTrace(StackTraceElement[] stackTrace) {
StringBuilder buffer = new StringBuilder();
for (StackTraceElement element : stackTrace) {
buffer.append(element).append(SEPARATOR);
}
return buffer.toString();
}
public static String formatCurrentStacktrace() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
return formatStackTrace(stackTrace);
}
}
Możesz użyć narzędzia jstack, jeśli chcesz sprawdzić bieżący stos wywołań procesu.
Usage:
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] <executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server)
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-24 09:24:16
To jest stary post, ale oto moje rozwiązanie:
Thread.currentThread().dumpStack();
Więcej informacji i więcej metod tam : http://javarevisited.blogspot.fr/2013/04/how-to-get-current-stack-trace-in-java-thread.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-25 03:14:51
Dla ludzi, którzy po prostu chcą mieć aktualny stacktrace do swoich dzienników, wybrałbym:
getLogger().debug("Message", new Throwable());
Cheers
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-02-03 11:41:28