Rabu, 09 Juli 2014

How to get current stack trace in Java for a Thread - Debugging Tutorial




Stack trace is very useful while debugging or troubleshooting any issue in
Java. Thankfully Java provides couple of ways to get 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 some time provide vital clue on missing or incorrect argument. Now, When I
have couple of Java debugging tips at my
sleeve, I still think knowledge of  how
to get current stack trace in Java for any Thread, or simply print stack trace
from any point in code worth knowing. For those who are not very familiar with stack
trace and wondering what is stack trace in Java, here is quick revision.
Thread execut
es 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




How to get current thread stack trace in Java for exception

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
from Thread 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.






import java.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
*/

public class StackTraceExample {
private static final Logger logger = Logger.getLogger(StringReplace.class.getName());

public static void main(String args[]) {

//calling a method to print stack trace further down
first();
}

public static void first(){
second();
}

private static void second() {
third();
}

private static void third() {

//If you want to print stack trace on console than use dumpStack() method
System.err.println("Stack trace of current thread using dumpStack() method");
Thread.currentThread().dumpStack();

//This is another way to print stack trace from current method
System.err.println("Printing stack trace using printStackTrace() method of Throwable ");
new Throwable().printStackTrace();

//If you want stack trace as StackTraceElement in program itself than
//use getStackTrace() method of Thread class
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

//Once you get StackTraceElement you can also print it to console
System.err.println("displaying Stack trace from StackTraceElement in Java");
for(StackTraceElement st : stackTrace){
// System.err.println(st);
}

}

}
Output
Stack 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 Throwable
java.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.



























Source:http://javarevisited.blogspot.com/2013/04/how-to-get-current-stack-trace-in-java-thread.html

Tidak ada komentar:

Posting Komentar