Pages

The stack trace is very useful while debugging or troubleshooting any issue in Java. Thankfully Java provides a couple of ways to get a current stack trace of a Thread in Java, which can be really handy in debugging. When I was new to Java programming, and didn’t know how to remote debug a Java application, I used to put debug code as patch release, which uses classpath to pick debug code for printing stack trace and shows how a particular method is get called and that sometimes provide vital clue on missing or incorrect argument. Now, When I have a couple of Java debugging tips at my sleeve, I still think knowledge of how to get a current stack trace in Java for any Thread, or simply print the stack trace from any point in code worth knowing. For those who are not very familiar with stack trace and wondering what is the stack trace in Java, here is the quick revision. The thread executes code in Java, they call methods, and when they call, they keep them in there stack memory. You can print that stack trace to find out, from where a particular method is get called in execution flow. This is especially useful if you are using lot of open source libraries, and don’t have control on all the code. One of the most familiar face of stack trace in Java is printing stack trace using Exception.printStackTrace(), when an Exception or Error is thrown in Java. In this Java tutorial, we will look at couple of ways to print and get current stack trace of a Thread in Java.

Stack Trace for current Thread in Java

One of the easiest way of printing stack trace of current thread in Java is by using dumpStack()method from java.lang.Thread class. This method prints stack trace of thread on which it get's called. You can use Thread.currentThread() method to get reference of current thread before calling this method. Another way printing stack trace is using printStackTrace() method of Throwable class. Just use new Throwable().printStackTrace() method ,and it will print complete stack trace from where a method is called, into console. Main difference between using dumpStack() and printStackTrace() is first entry in Stack, In case of dumpStack() first entry is always java.lang.Thread.dumpStack(), while in later case it's the method from where you printed stack trace. If you don't want to print stack trace and rather wants it in Java program, you can use getStackTrace() method fromThread class. This method returns an array of StackTraceElement. In following Java program we will see examples of all three approaches to get stack trace of current thread in Java.

importjava.util.logging.Logger;
/**
* Java program to demonstrate how to print and get stack trace for current
* thread in Java. Stack trace are useful information while debugging or
* troubleshooting any issue.
*
* @author Javin Paul
*/publicclassStackTraceExample {
privatestaticfinalLogger logger =Logger.getLogger(StringReplace.class.getName());
publicstaticvoidmain(Stringargs[]) {
//calling a method to print stack trace further down
first();
}
publicstaticvoidfirst(){
second();
}
privatestaticvoidsecond() {
third();
}
privatestaticvoidthird() {
//If you want to print stack trace on console than use dumpStack() methodSystem.err.println("Stack trace of current thread using dumpStack() method");
Thread.currentThread().dumpStack();
//This is another way to print stack trace from current methodSystem.err.println("Printing stack trace using printStackTrace() method of Throwable ");
newThrowable().printStackTrace();
//If you want stack trace as StackTraceElement in program itself than//use getStackTrace() method of Thread classStackTraceElement[] stackTrace =Thread.currentThread().getStackTrace();
//Once you get StackTraceElement you can also print it to consoleSystem.err.println("displaying Stack trace from StackTraceElement in Java");
for(StackTraceElement st : stackTrace){
// System.err.println(st);
}
}
}
OutputStack trace of current thread using dumpStack() method
java.lang.Exception:Stack trace
at java.lang.Thread.dumpStack(Thread.java:1249)
at test.StringReplace.third(StringReplace.java:38)
at test.StringReplace.second(StringReplace.java:31)
at test.StringReplace.first(StringReplace.java:27)
at test.StringReplace.main(StringReplace.java:23)
Printing stack trace using printStackTrace() method of Throwablejava.lang.Throwable
at test.StringReplace.third(StringReplace.java:42)
at test.StringReplace.second(StringReplace.java:31)
at test.StringReplace.first(StringReplace.java:27)
at test.StringReplace.main(StringReplace.java:23)
displaying Stack trace from StackTraceElement in Java

That's all on How to get current stack trace in Java. By using above methods you can get stack trace from current thread or any other Thread. If you are also interested on taking thread dump, than you can use kill -3 pid in UNIX environment and ctrl+break in Windows environment. This will instruct JVM to print stack trace of all threads in console.

3 comments
:

I see you are declaring logger, but not using it in the example. I say that, because I'm trying to solve the same problem - to print the stack trace - but want to print it in the logger ... The thing is that the method Thread.currentThread().getStackTrace() is available since Java 1.5, but I'm using Java 1.4. Any idea how to print the stack trace in the logger with Java 1.4?

The RoboGuice framework includes a logger class that uses this technique to find the class that is making the logging call. It is a logger that requires no configuration (i.e. you don't have to pass a class or string tag to it to tell it which logger it is). All you do is make static method calls.

See the source here: https://github.com/roboguice/roboguice/blob/master/roboguice/src/main/java/roboguice/util/Ln.java