There Are Great Tools in Your bin/ Directory
Every Java developer is familiar with javac
for compiling, java
for running, and probably jar
for packaging Java applications. However, many other useful tools come installed with the JDK. They are already on your computer in your JDK’s bin/
directory and are invokable from your PATH
. It’s good to get acquainted with some of these tools so you know what’s at your disposal:
jps
: If you’ve ever found yourself runningps aux | grep java
to find the running JVMs, you probably just want to runjps
. This dedicated tool lists all the running JVMs, but instead of showing you a lengthy command withCLASSPATH
s and arguments,jps
simply lists the process ID and the application’s main class name, making it far easier to figure out which process is which.jps -l
will list the fully qualified main class name,jps -m
will show the arguments passed to the main method, andjps -v
will show all the arguments passed to the JVM itself.javap
: The JDK comes with a Java class file disassembler. Runjavap <class file>
to see that class file’s fields and methods, which can often be very enlightening for understanding what code written in JVM-based languages such as Scala, Clojure, or Groovy is turned into under the hood. Runjavap -c <class file>
to see the complete bytecode of those methods.jmap
: Runningjmap -heap <process id>
will print a summary of the JVM process’s memory space, such as how much memory is being used in each of the JVM’s memory generations, as well as the heap configuration and type of GC being used.jmap -histo <process id>
will print a histogram of each class in the heap, how many instances there are of that class, and how many bytes are of memory are consumed. Most critically, runningjmap -dump:format=b,file=<filename> <process id>
will dump a snapshot of the entire heap to a file.jhat
: Runningjhat <heap dump file>
will take the file generated byjmap
and run a local web server. You can connect to this server in a browser to explore the heap space interactively, grouped by package name. The “Show instance counts for all classes (excluding platform)” link shows only instances of classes outside of Java itself. You can also run “OQL” queries, allowing you to query the heap space via SQL-esque syntax.jinfo
: Runjinfo <process id>
to see all system properties the JVM loaded with and JVM command-line flags.jstack
: Runningjstack <process id>
will print stack traces for all current Java threads running in a JVM.jconsole
andjvisualvm
: These are graphical tools that allow connecting to JVMs and interactively monitoring running JVMs. They offer visual graphs and histograms of various aspects of a running process and are a mouse-friendly alternative to many of the tools listed above.jshell
: As of Java 9, Java has an honest-to-goodness REPL — a great tool to check syntax, do quick Java-based commands, or try out code and experiment without building full programs.
Many of these tools can run not only locally, but against JVM processes running on remote machines as well. These are only some of the useful programs you already have installed, take some time to see what else is in your JDK’s directory of executables and read their man
pages— it’s always handy to know what tools are in your toolbelt.