1

In Java, I want to get the stack trace of how the calls were made to which methods. How can I achieve that?

For example, my call stack is:

class Example {
   public void init() {
      func1();
      // TODO : now here I want to print the stack trace that it went from:
      // func1
      // func2
      // func3
   }
   public void func1() {
      func2();
   }
   public void func2() {
      func3();
   }
   public void func3() {
      // some code here
   }
    

}
5
  • 1
    Once you're in the caller, the ship has sailed. You can only do this inside the funcX() methods. Commented Jun 30, 2022 at 18:27
  • You can maintain a stack yourself by having a custom variable of type Stack (of string) and PUSH (say function name) to stack right before you call it. and then POP all of them where you want. Commented Jun 30, 2022 at 18:40
  • Do you control the other methods (func1, func2, etc)? Are you able to add code to those other methods? Commented Jun 30, 2022 at 22:19
  • No I dont necessarily control the other methods. I cant add code to them Commented Jul 1, 2022 at 4:41
  • Let's rule out the possibility that this may be a XY problem. Why do you need the list of methods that were called? Commented Jul 1, 2022 at 6:09

2 Answers 2

1

This is what you want

class Example {
  public void init() {
     func1();
     // TODO : now here I want to print the stack trace that it went from:
     try { throw new Throwable("Debugging") } catch (Throwable t) { t.printStackTrace(); }
     // or
     System.out.println(Arrays.toString(Thread.currentThread().getStackTrace()));
     // func1
     // func2
     // func3
  }
  public void func1() {
     func2();
  }
  public void func2() {
     func3();
  }
  public void func3() {
     // some code here
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thats not what I want. I want to know which all methods did the init() method call
Actually your code will just print init and the method that called it. It will not print func1 nor func2 nor func3. It always intrigues me why people up-vote an answer that does not answer the question. By the way, the code in your answer does not compile. It is missing a semicolon in the try block. Did you test your code before posting? Or did you make a mistake when you copied your code to the answer?
The first example does not need a try block, it is same as new Throwable("Debugging").printStackTrace();
@DuncG or just use Thread#dumpStack()
0

Use a IDE and make use of breakpoints. Personally, I use IntelliJ. You can add breakpoints to your code. Breakpoints will make your program halt when they get hit. At that time you can see call stack in Debugger window under IntelliJ.
IntelliJ provides great support for debugging, and also provides easy tutorials to get started with IntelliJ Debugger Tool.

If you want to print Stack Trace, then make use of Thread.currentThread.getStackTrace() function.

1 Comment

This will work in small projects or when you can look at the debugger. Printing a stacktrace can go into a logfile for later evaluation also, so your contribution does not answer the question.

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.