Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2012 Google, Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *  Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

import java.util.Map.Entry;

public class Main {
    /**
     * Get a string representation of the current stack state of all the active threads.
     */
    public static String getStackTraces() {
        StringWriter stringWriter = new StringWriter(5000);
        printStackTraces(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    /**
     * Print a string representation of the current stack state of all the active threads.
     */
    public static void printStackTraces() {
        printStackTraces(new PrintWriter(new OutputStreamWriter(System.err)));
    }

    /**
     * Print a string representation of the current stack state of all the active threads.
     */
    public static void printStackTraces(PrintWriter writer) {
        Map<Thread, StackTraceElement[]> map;
        try {
            map = Thread.getAllStackTraces();
        } catch (Throwable e) {
            writer.println("Failed to obtain stack traces: " + e);
            return;
        }
        if (map == null) {
            writer.println("No stack traces available");
            return;
        }
        for (Entry<Thread, StackTraceElement[]> entry : map.entrySet())
            printStackTrace(writer, entry.getKey(), entry.getValue());
        writer.flush();
    }

    private static void printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace) {
        try {
            writer.println(thread.toString() + ":");
            for (int i = 0; i < trace.length; i++)
                writer.println("\tat " + trace[i]);
        } catch (Exception e) {
            writer.println("\t*** Exception printing stack trace: " + e);
        }
        writer.flush();
    }
}