How to display stack trace information?
For example,
class ClassUtil {
public String fullyQualifiedName;
public ClassUtil(String fullyQualifiedName) {
super();
if (fullyQualifiedName == null)
this.fullyQualifiedName = "";
else {
this.fullyQualifiedName = fullyQualifiedName.trim();
}
}
public ClassUtil(Class c) {
super();
this.fullyQualifiedName = c.getName();
}
public String getFullClassName() {
return this.fullyQualifiedName;
}
public String getPackageName() {
int lastDot = fullyQualifiedName.lastIndexOf ('.');
return (lastDot<=0)?"":fullyQualifiedName.substring (0, lastDot);
}
public String getClassName() {
int lastDot = fullyQualifiedName.lastIndexOf ('.');
return (lastDot<=0)?fullyQualifiedName:fullyQualifiedName.substring (++lastDot);
}
}
public class StackTraceDisplay {
public static void main(String[] args) {
doFun1();
}
public static void doFun1(){
doFun2();
}
public static void doFun2(){
displayStackTrace(Thread.currentThread().getStackTrace());
}
public static void displayStackTrace(StackTraceElement e[]) {
for (StackTraceElement s : e) {
if (s.getMethodName().equals("getStackTrace"))
continue;
System.out.println ("Filename: " + s.getFileName());
System.out.println ("Line number: " + s.getLineNumber());
ClassUtil cu = new ClassUtil(s.getClassName());
System.out.println ("Package name: " + cu.getPackageName());
System.out.println ("Full Class name: " + cu.getFullClassName());
System.out.println ("Classlass name: " + cu.getClassName());
System.out.println ("Method name: " + s.getMethodName());
System.out.println ("Native method?: " + s.isNativeMethod());
System.out.println ("toString(): " + s.toString());
System.out.println ("");
}
}
}
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- What are class variables in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?