java stack dump on windows

后端 未结 7 852
自闭症患者
自闭症患者 2020-11-30 02:19

I have a running java process in a standard windows command window. ie i have run \'cmd\' and typed in java -jar ...

I need to be able to get a full stack dump of al

相关标签:
7条回答
  • 2020-11-30 03:24

    Other posters are correct - ctrl-break in the console or jstack.

    Otherwise, if you're using Java 1.5 or above, you can get hold of this information programmatically at runtime, via:

    Thread.getAllStackTraces() (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#getAllStackTraces())

    then iterate through the results.

    A little long-winded, but something like this:

    Map<Thread,StackTraceElement[]> traces = Thread.getAllStackTraces();
    Iterator<Thread> i = traces.keySet().iterator();
    while (i.hasNext()) {
       Thread thd = i.next();
       System.out.println("*** Thread id"+thd.getId()+":"+thd.getName()+" ***");
       StackTraceElement[] trace = traces.get(thd);
       for (int j=0; j < trace.length; ++j) {
          System.out.println(trace[j]);
       }
       System.out.println();
    }
    

    You can then choose whatever method you want to trap your program to cause this to happen, and format/direct the output.

    A disadvantage of this method is that it isn't exact, in that the stacks of the different threads aren't guaranteed to have been taken at exactly the same time. However, there's an associated performance advantage in that your entire process won't be suspended while the snapshot is taken.

    0 讨论(0)
提交回复
热议问题