0

Suppose I ran a java program using the java command, is it possible to get the current position or stacktrace of the this program while it is running if I have the source code of that program? A solution which does not stop my program is preferred.

keelar
  • 5,814
  • 7
  • 40
  • 79

2 Answers2

2

Attach a debugger to the JVM and pause the JVM. The debugger can then show you the stacktrace.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
2

jps to find the process id. jstack to dump the stack.

If it is running for the command line the ctrl-Z/ctrl-break (depending on platform) will also do the job.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Btw, look like ctrl-Z does not work for my platform (Red Hat linux 4.1.2-51), it seems like just putting my program into background, and I found under ctrl-Z, I can't even use jstack to spy it. – keelar Jun 24 '13 at 23:04
  • Er, `ctrl-3` I think was the alternative, but probably does the same? Usually `ctrl-z` would suspend (with `bg` to resume). Possibly something different is done with the signal handling. – Tom Hawtin - tackline Jun 24 '13 at 23:54
  • Thank you. I think it's `fg` to resume, isn't it? – keelar Jun 24 '13 at 23:55
  • `fg` resumes on foreground, `bg` resumes on background. Another alternative is to issue a SIGQUIT (`kill -3 [pid]`), which will dump all the threads (and their stacktraces) to the STDOUT. – Bruno Reis Jun 25 '13 at 00:50