Published on Mar 15 2025 in Java

How to Create a Java Thread Dump Using Command Line

A Java thread dump is a snapshot of all active threads in a Java application, useful for diagnosing performance issues, deadlocks, or application hangs. This tutorial explains how to generate a thread dump using the command line with JDK 22.

1. Identify the Java Process (PID)

Before generating a thread dump, you need to find the Process ID (PID) of the running Java application.

Method 1: Using jps

JDK provides the jps (Java Process Status) tool to list active Java processes:

jps -l

This command outputs a list of Java processes with their PIDs. Example output:

12345 my.application.Main
67890 org.apache.catalina.startup.Bootstrap

Here, 12345 is the PID of the Java application.

Method 2: Using ps (Linux/macOS)

If jps is unavailable, use the ps command:

ps aux | grep java

This will list all Java processes along with their PIDs.

2. Generate the Thread Dump

Once you have identified the PID of the Java process, use one of the following methods to generate a thread dump.

jstack is a built-in JDK tool that provides a detailed thread dump.

jstack -l <PID>

Example:

jstack -l 12345 > thread_dump.txt

This saves the thread dump into a file for further analysis.

Method 2: Using kill -3 (Linux/macOS)

You can send the SIGQUIT signal to the Java process, which causes the JVM to print a thread dump to the standard output (console or logs).

kill -3 <PID>

Example:

kill -3 12345

Check the application logs or console output for the generated dump.

Method 3: Using jcmd

The jcmd tool is another JDK utility that can generate a thread dump.

jcmd <PID> Thread.print

Example:

jcmd 12345 Thread.print > thread_dump.txt

This saves the thread dump into a file.

3. Analyze the Thread Dump

After generating the thread dump, you can analyze it using tools like:

To find out possible issues:

Conclusion

Generating a Java thread dump is crucial for troubleshooting performance bottlenecks and diagnosing deadlocks. Using JDK 22, you can create a thread dump easily with jstack, jcmd, or kill -3 commands.