Backend Development 8 min read

Master JDK Command-Line Tools for Java Troubleshooting

This guide explains how to use JDK command-line utilities such as jps, jstat, jinfo, jmap, jhat, and jstack to monitor JVM processes, inspect memory usage, generate heap dumps, and diagnose performance issues, providing command syntax, options, and practical examples.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master JDK Command-Line Tools for Java Troubleshooting

JDK Command-Line Tools Overview

Environment: JDK 1.8.0_92. The JDK (Java Development Kit) includes a set of command-line tools that help developers troubleshoot Java applications.

Commonly Used Tools

jps – Displays JVM process IDs (PIDs) and can show full package and class names with the -l option or launch arguments with -m . Useful for locating running Java processes.

jstat – Monitors runtime JVM information such as class loading, memory usage, and garbage collection. Options include -gc , -class , -compiler , and many others to view specific metrics.

jstack – Retrieves thread stack traces of a JVM process, aiding in diagnosing hangs or crashes. Options like -F , -l , and -m provide additional details.

jmap – Generates heap dump files for memory analysis. The -dump option creates a snapshot, while -heap and -histo display heap details and object statistics.

Additional tools such as jconsole and jvisualvm are also available for troubleshooting.

Tool Location

All tools reside in the %JAVA_HOME%\bin directory. Their executable size is typically around 17 KB because they are thin wrappers around classes in tools.jar .

Tool Details

1. jps

Displays all currently running Java virtual machine processes.

Options:

-l : Output full package and class name, or JAR path if applicable.

-m : Show arguments passed to the main method.

Example program:

<code>public class JpsMain {
  public static void main(String[] args) throws Exception {
    System.out.println(java.util.Arrays.toString(args));
    System.in.read();
  }
}</code>

Running with parameters -v displays JVM launch arguments.

2. jstat

Monitors various JVM runtime information such as class loading, memory, and garbage collection.

Usage:

<code>jstat -&lt;option&gt; [-t] [-h&lt;lines&gt;] &lt;vmid&gt; [&lt;interval&gt; [&lt;count&gt;]]</code>

Key options:

-class : Shows class loading/unloading counts and times.

-compiler : Displays JIT compilation statistics.

-gc : Monitors heap usage and GC times.

-gcutil : Shows used space as a percentage of total.

Example to report GC every 3 seconds:

<code>jstat -gc 16480 3s</code>

3. jinfo

Shows and modifies JVM parameters in real time.

<code>jinfo option pid</code>

Example: display all JVM information for a process.

4. jmap

Generates heap dump snapshots and provides heap statistics.

<code>jmap -dump:live,format=b,file=heap.bin &lt;pid&gt;</code>

Other useful commands:

-heap : Detailed heap information.

-histo : Object histogram.

5. jhat

Analyzes heap dump files generated by jmap .

<code>jhat &lt;file&gt;</code>

Starts a web service (default port 7000) to explore the heap.

6. jstack

Creates a snapshot of all threads in a JVM process.

<code>jstack [option] &lt;pid&gt;</code>

Options:

-F : Force thread dump if the JVM is unresponsive.

-l : Include lock information.

-m : Show native stack for C/C++ frames.

End of guide.

JavaJDKTroubleshootingcommand line toolsjstackjmapjpsjstat
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.