Is there anyway to have a java application dump a file with information on why the JVM crashed?
-
Good question but what are you going to do about it? Are you a JVM developper? If the *JVM* crashes, the JVM is pretty much faulty. Here's a (1K view) question I asked about what to do with a reproducable JVM crash I can trigger at will: http://stackoverflow.com/questions/2299250 – SyntaxT3rr0r Sep 22 '10 at 02:14
-
I am dealing with java bytecode. And it seems when there is an invalid function java likes to crash without warning. It might be that the error is being handled somewhere. Is there anyway to get the last java exception thrown by a thread? – Will Sep 22 '10 at 13:48
5 Answers
If you do not mean that the actual JVM crashed with a program error, but more that you got an Exception which brought your program down unexpectedly, you can use a try-catch in your main method:
public static void main(String args[]) {
try {
doStuff();
} catch (Throwable e) {
e.printStackTrace();
logStuff(e);
}
}

- 73,784
- 33
- 194
- 347
-
This would catch any kind of `Error` including `OutOfMemoryError` which itself would require some memory to handle... Sounds like a bad idea to me. – whiskeysierra Sep 22 '10 at 07:24
-
-
-
Marked this as the answer because the problem was that I used Exception instead of Throwable. – Will Sep 22 '10 at 18:44
-
Oddly enough the Throwable handler above the try/catch I had did not catch it. Does Java not rethrow exceptions? – Will Sep 22 '10 at 18:46
-
Only if explicitly asked to. If this one is not the outermost method, then change to `logStuff(e); throw e; ` – Thorbjørn Ravn Andersen Sep 22 '10 at 21:31
If the JVM crashes due to error in native code (that is, code accessed via JNI - not necessarily the JVM itself), the JVM (usually) produces a heap dump. It's, by default, created in a file called hs_err_pidXXXX.log
in the working directory of the JVM (XXXX
is the process ID).
If you mean crashes from the Java program itself, here's a good article on how to set up handlers for uncaught exceptions. You can then log them (and the associated stack trace) to a file.

- 36,252
- 29
- 106
- 169
-
What if the exception is caught and handled by a try/catch that doesn't log it? Is there any way to get the last java exception thrown by a thread? – Will Sep 22 '10 at 14:35
-
@High: I don't think there is. The try/catch block is responsible for handling the exception and acting appropriately (for example: cancel an operation and/or log something). That's why one should (almost) never use empty catch blocks. But even "swallowing" exceptions definitely doesn't make the JVM crash or a thread die. Only uncaught exceptions could do so. – Joonas Pulakka Sep 23 '10 at 07:50
You can use the DebugDiag tool or the IBM Memory Analyzer. There are plenty of tutorials on the latter. Hope this helps.

- 1,916
- 15
- 23
In jdk you have a tool called jmap to dump the memory and jhat to analyse the objects .

- 12,427
- 23
- 80
- 112
-
How would you dump if it is ending? I guess I could add a Thread.Sleep at the end, but is there a better way? – Will Sep 22 '10 at 16:22
-