javathread-dumpjstack

How to redirect jstack output to a file?


I have tried jstack -l <PID> >> dump.log and the dump.log file created with no content. Although I could see the thread dump in the terminal where the Java application was running but I need the thread dump in a file. Moreover there is a message at the jstack output: Unable to open socket file: target process not responding or HotSpot VM not loaded.

I tried running java and jstack both by the system user but still no luck getting the pitut redirected to a file. I have used both java 8 and 9.

PS: I have googled about this issue and found people experiencing inconsistencies with having an outut file of jstack or jmap. Kindly let me know if you have any working solution to the problem.


Solution

  • You can get the thread dump by the following command:

    sudo jstack <PID> > a.txt
    

    The above should work because you mentioned that you can see the thread dump on the terminal.

    But you also mentioned that jstack outputs

    Unable to open socket file: target process not responding or HotSpot VM not loaded.

    Hence, there might be a problem with your process itself, and I think it is not loaded properly. Resolve that issue, and the jstack should work for you.


    Another issue might be that you might have exhausted the number of open files in your system. Run the following 2 commands.

    If the output is same for both of the above commands, you probably have exhaused the total file open limits. And hence, you are not able to write the output into a file. Close the open fd's and that should solve the issue.