1

Is there anyway to have a java application dump a file with information on why the JVM crashed?

rob199
  • 25
  • 6
Will
  • 10,013
  • 9
  • 45
  • 77
  • 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 Answers5

1

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);
  }
}
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

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 (XXXXis 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.

Joonas Pulakka
  • 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
0
miku
  • 181,842
  • 47
  • 306
  • 310
0

You can use the DebugDiag tool or the IBM Memory Analyzer. There are plenty of tutorials on the latter. Hope this helps.

Sagar V
  • 1,916
  • 15
  • 23
0

In jdk you have a tool called jmap to dump the memory and jhat to analyse the objects .

Dead Programmer
  • 12,427
  • 23
  • 80
  • 112