My Wiki

July 25, 2006

Convert exception stacktrace to String object

Filed under: Java — jaikiran @ 12:31 pm

Usually whenever a exception occurs, we use the method printStackTrace() on the exception object to display the stacktrace. However, if the stacktrace has to be stored into a String  object then the following piece of code will prove handy:

  /**
     * Creates and returns a {@link java.lang.String} from <code>t</code>’s stacktrace 
     * @param t Throwable whose stack trace is required
     * @return String representing the stack trace of the exception
     */
    public String getStackTrace(Throwable t) {
        StringWriter stringWritter = new StringWriter();
        PrintWriter printWritter = new PrintWriter(stringWritter, true);
        t.printStackTrace(printWritter);
        printWritter.flush();
        stringWritter.flush();
 

       return stringWritter.toString();
    }

How to enable Garbage Collection (GC) logs

Filed under: Java — jaikiran @ 10:19 am

To enable GC logs, the -Xloggc:logFileName option will have to be passed when java command is being executed. Additionally if the detailed log of the GC is required, then an additional -XX:+PrintGCDetails option will have to be passed.

Example: java -Xloggc:D:/log/myLogFile.log -XX:+PrintGCDetails myProg

Create a free website or blog at WordPress.com.