Transferred from: http://www.cnblogs.com/AloneSword/p/3857423.html
In Java, you can print stacks directly from tools like eclipse, but you need to know the stack when you can't use eclipse tools in some environments, how do you handle it?
There are 3 ways to choose from:
Method One:
PackageName.xu;PublicClasscallstack {PublicStaticvoid Printcallstatck () {throwable ex = new Throwable (); Stacktraceelement[] stackelements = Ex.getstacktrace (); if (stackelements! = null ) {for (int i = 0; i < Stackelements.length; I++); System.out.print (Stackelements[i].getfilename () + "/t" ); System.out.print (Stackelements[i].getlinenumber () + "/t" ); System.out.println (Stackelements[i].getmethodname ()); System.out.println ("-----------------------------------"
Method Two:
New Exception ("This is a log"); E.printstacktrace ();
Method Three:
String fullstacktrace = Org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace (e)
Method Four:
Thread.CurrentThread (). Getstacktrace ()
Personal recommendation, method One, reason: easy to use, fast
Resources:
1, http://blog.csdn.net/chief1985/article/details/4618492
2, http://www.cnblogs.com/flyme/archive/2012/04/10/2440029.html
3, Http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java " recommend a closer look"
Several methods of Java print stack