Source-code:public Class A {public A () {}private static void Printstacktrace () {
stacktraceelement[] stackelements = new Throwable (). Getstacktrace ();if (stackelements! = null) {for (int i = 0; i < stackelements.length; i++) {
System.out.println ("" + stackelements[i]);}}} public void MethodA () {MethodB ();} public void MethodB () {printstacktrace ();} public static void Main (string[] argv) {A A = new A (); A.methoda (); } }
Output: A.printstacktrace (A.java:5) A.methodb (a.java:16) A.methoda (a.java:15) A.main (a.java:20) Note:
stacktraceelement. Java Public String
toString() {return getclassname () + "." + MethodName + (Isnativemethod ()? "(Native Method)": (FileName = null && linenumber >= 0?) "(" + filename + ":" + linenumber + ")": (FileName = null?) "(" +filename+ ")": "(Unknown Source))); In addition, the following methods are also available: New Exception ("Hello"). Printstacktrace (); Thread.getstacktrace ()/thread.getallstacktraces () since JDK 1.5thread.currentthread (). DumpStack ()
Print Call Stack in Java