
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get stack trace using thread in Java 9?
Java 9 has added StackWalker class to provide a standard API for accessing the current thread stack. In the previous java versions, we can use Throwable::getStackTrace, Thread::getStackTrace, and SecurityManager:: GetClassContext provided methods to obtain the thread stack.
Thread.getStackTrace() method will return an array of stack trace elements representing the stack dump of a thread (StackTraceElement[]). The first element of an array represents the top of a stack, it can be the last method invocation in a sequence, and the last element of an array represents the bottom of a stack, it can be the first method invocation in a sequence.
Syntax
public StackTraceElement[] getStackTrace()
Example
import java.lang.StackWalker.Option; public class GetStackTraceTest { public static void main(String args[]) { GetStackTraceTest.testPrintCurrnentStackTrace(); GetStackTraceTest.testShowReflectFrames(); } // get StackTrace using Thread public static void testPrintCurrnentStackTrace() { StackTraceElement[] stack = Thread.currentThread().getStackTrace(); for(StackTraceElement element : stack) { System.out.println(element); } } // SHOW_REFLECT_FRAMES public static void print(StackWalker stackWalker) { stackWalker.forEach(stackFrame -> System.out.printf("%6d| %s -> %s %n", stackFrame.getLineNumber(), stackFrame.getClassName(), stackFrame.getMethodName())); } public static void testShowReflectFrames() { final StackWalker stackWalker = StackWalker.getInstance(Option.SHOW_REFLECT_FRAMES); print(stackWalker); } }
Output
java.base/java.lang.Thread.getStackTrace(Thread.java:1654) GetStackTraceTest.testPrintCurrnentStackTrace(GetStackTraceTest.java:10) GetStackTraceTest.main(GetStackTraceTest.java:5) 17| GetStackTraceTest -> print 25| GetStackTraceTest -> testShowReflectFrames 6| GetStackTraceTest -> main
- Related Questions & Answers
- Print stack trace in Java
- How to display all stack frames of the current thread in Java 9?
- Place Stack Trace into a String in Java
- How to filter stack frames using StackWalker API in Java 9?
- How can I get a stack trace for a JavaScript exception?
- Java Program to Convert a Stack Trace to a String
- How to rethrow InnerException without losing stack trace in C#?
- How to create a thread in JShell in Java 9?
- How can I get a JavaScript stack trace when I throw an exception?
- How to get dates using LocalDate.datesUntil() method in Java 9?
- How to access each stack element of StackWalker in Java 9?
- Get current thread in Java
- Get and Set the stack size of thread attribute in C
- How to get JShell documentation in Java 9?
- How to get the thread ID from a thread in C#?
Advertisements