17

When you debug a complex Java code which has some parallel threads running, it is not easy to capture all the breakpoints and sometimes it is really hard to find which path has caused the problem. In such a scenarios, we need to print the method call stack at suspected locations. How can I print the method call stack within Java?

1
  • 5
    You're better off using a real IDE like IntelliJ. It shows you the complete stack and more. Commented Aug 29, 2017 at 17:18

3 Answers 3

37

Here is how you print the stack trace from a given location in your source file.

System.out.println("Printing stack trace:");
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (int i = 1; i < elements.length; i++) {
     StackTraceElement s = elements[i];
     System.out.println("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
}
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think this gives you any more information than using the debugger in an IDE would give you. Also, it doesn't give you the opportunity to let you see the values of your variables, in the way a debugger would. I can't imagine any circumstance in which this would be useful. It rather looks to me that you've brought a bicycle to the Formula One.
Correct, but not very useful.
even easier new Throwable().printStacktrace()
To all the people who say that this isn't very useful: The OP said that this is to debug a multi-threading issues ("parallel threads"). Using the debugger, one might not be able to catch a combination of threads in the exact state to cause an issue. So, the combination of how this code and how it is triggered can be very useful. (The debugger has some ability with conditional breakpoints to get close to an exact multi-threading condition, but there are more scenarios to get stack traces based on multiple thread states.)
Also, you can log stacktraces to log files and wait for some edge case to happen before checking the logs, which you definitely can't do in an IDE, so definitely very useful.
10

With Java 8

Arrays.stream(Thread.currentThread().getStackTrace()).forEach(s -> System.out.println(
    "\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s
        .getLineNumber() + ")"));

Comments

4

Just use below code

System.out.println("Stack trace:");
StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
for (int i = 1; i < stackTraces.length; i++) {
     System.out.println(stackTraces[i]);
}

This is definition of toString method of StackTraceElement

public String toString() {
    return getClassName() + "." + methodName +
        (isNativeMethod() ? "(Native Method)" :
         (fileName != null && lineNumber >= 0 ?
          "(" + fileName + ":" + lineNumber + ")" :
          (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));
}

So you can use directly to print StackTraceElement.

1 Comment

Why downvoting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.