tags:

views:

281

answers:

7

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 all threads if at all possible.

i remember that under linux you can send a message to the JVM via an option on the quit command.

in this document sun state

To generate a stack trace on Windows 95, or Windows NT platforms, enter the key sequence in the window where the Java program is running, or click the Close button on the window.

this is clearly wrong, as closing the terminal does nothing but kill the process and close the window.

+4  A: 

Typing Ctrl+Break is the correct way to generate a thread dump on Windows.

Are you pressing Ctrl+C (=interrupt) maybe? That will send a SIGINT, which will generally kill your process.

Seth
+5  A: 

You can use jstack [ option ] pid (if the question is about thread dump). Use jps to find the id of your Java process. See Using jstack on Windows for some more details on how to use it.

Pascal Thivent
A: 

In addition to Seth's answer, you could also launch a JConsole on your machine in order to inspect each thread's stack trace. This has the added benefit of being able to detect deadlocks and monitor memory usage.

Adamski
A: 

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.

A: 

In the Java 6 JDK the jvisualvm executable allows you to attach to a running program (double click on its entry in the left side).

When attached, there is a Threads pane on the right side, which has a Thread Dump button.

This gives you a thread dump.

Notes jvisualvm also allows you to take snapshots of the whole application for later analyzis.

Thorbjørn Ravn Andersen
A: 

Take a look at this Stack Overflow post

http://stackoverflow.com/questions/239544/thread-dump-programmatically-jdi-java-debugger-interface

We have implemented a JMX method to dump stack traces. This is really handy because you can examine stack traces in a web browser even on a remote machine.

Fortyrunner