views:

3033

answers:

6

Is it possible to get a thread dump of a Java Web Start application? And if so, how?

It would be nice if there were a simple solution, which would enable a non-developer (customer) to create a thread dump. Alternatively, is it possible to create a thread dump programmatically?

In the Java Web Start Console I can get a list of threads by pressing 't' but stacktraces are not included.

If answers require certain java versions, please say so.

+2  A: 

Try

StackTraceElement[] stack = Thread.currentThread().getStackTrace();

Then you can iterate over the collection to show the top x stack elements you're interested in.

Amir Arad
+2  A: 

Recent JDKs (sadly not JREs) include tools like jstack which does such things. JVMs from version 5 include JMX extensions to get thread dumps, memory statistics, and much more. All java applications, including web start applications, have this functionality available.

You would either need to have the JDK installed or to write a JMX client that does the same thing. Take a look at http://java.sun.com/javase/6/docs/technotes/guides/management/ to get more information.

Asgeir S. Nilsen
A: 

Since Java 5 you have the getStackTrace() method of Thread class. For prior versions you can do:

Thread.currentThread().dumpStack();

This will print the stack trace to System.out

Marcio Aguiar
That only gives for the current thread, not all threads.
Thorbjørn Ravn Andersen
+2  A: 

Since 1.5 you can use Thread.getAllStackTraces() to get a Map to iterate over.

The ideal output would be that produced from Ctrl-\ (or Ctrl-Break or similar), but there doesn't seem to be a documented way of producing this. If you are willing to limit yourself to sun's JVM (or use reflection I suppose) you could have a dig around the sun.* packages and see if anything interesting shows up.

ashirley
+5  A: 

In the console, press V rather than T:

t:   dump thread list
v:   dump thread stack

This works under JDK6. Don't know about others.

Alternative, under JDK5 (and possibly earlier) you can send a full stack trace of all threads to standard out:

Under Windows: type ctrl-break in the Java console.

Under Unix: kill -3 <java_process_id> (e.g. kill -3 5555). This will NOT kill your app.

One other thing: As others say, you can get the stacks programatically via the Thread class but watch out for Thread.getAllStackTraces() prior to JDK6 as there's a memory leak.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434648

Regards,

scotty

scotty
A: 

these answers suck.

captain java