ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward
Learn about some useful and helpful JDK tools
Discover where to find more information about each of them
See a few of them in action
jar
- create an archive for classes and resources, and manipulate or restore individual classes or resources from an archive
jarsigner
- sign and verify Java Archive (JAR) files
javac
- read Java class and interface definitions and compile them into bytecode and class files
javadoc
- generate HTML pages of API documentation from Java source files
jdeprscan
- static analysis tool that scans for uses of deprecated API elements
jlink
- assemble and optimize a set of modules and their dependencies into a custom runtime image
jmod
- create JMOD files and list the content of existing JMOD files
keytool
- manage a keystore (database) of cryptographic keys, X.509 certificate chains, and trusted certificates
serialver
- return the serialVersionUID
for one or more classes in a form suitable for copying into an evolving class
java
- launch a Java application
jdeps
- launch the Java class dependency analyzer
jpackage
- package a self-contained Java application
jshell
- interactively evaluate declarations, statements, and expressions of the Java programming language in a REPL
jrunscript
- run a command-line script shell that supports interactive and batch modes
jwebserver
- launch the Java Simple Web Server
rmiregistry
- create and start a remote object registry on the specified port on the current host
javap
- disassemble one or more class files
jdb
- find and fix bugs in Java platform programs
jhsdb
- attach to a Java process or launch a postmortem debugger to analyze the content of a core dump from a crashed JVM
jmap
- print details of a specified process
jnativescan
- static analysis tool that scans one or more jar files for uses of native functionalities
jstack
- print Java stack traces of Java threads for a specified Java process
jcmd
- send diagnostic command requests to a running Java Virtual Machine (JVM)
jconsole
- start a graphical console to monitor and manage Java applications
jfr
- parse and print Flight Recorder files
jinfo
- generate Java configuration information for a specified Java process
jps
- list the instrumented JVMs on the target system
jstat
- monitor JVM statistics
jstatd
- monitor the creation and termination of instrumented Java HotSpot VMs
https://docs.oracle.com/en/java/javase/17/docs/specs/man/jar.html
general-purpose archiving/compression tool based on ZIP/ZLIB
preferred packaging/deployment mechanism since Java 1.1
heavily influenced by TAR (Tape ARchive); hence JAR (Java ARchive)
CLI tool uses Unix-style CLI parameters
built using primarily java.base
:java.util.jar
APIs
https://docs.oracle.com/en/java/javase/17/docs/specs/jar/jar.html
-c
| --create
: Creates the archive
-t
| --list
: Lists the contents for the archive
-u
| --update
: Updates an existing JAR file
-x
| --extract
: Extracts contents of a JAR file
-v
| --verbose
: Print verbose output to stdout
-f=FILE
| --file=FILE
: specifies the archive filename FILE
-C
DIR: changes to the specified directory and includes the files specified
@
FILE: CLI options can be found in FILE
Examples
jar --create --file classes.jar Foo.class Bar.class
Creates classes.jar containing Foo.class and Bar.class
jar -cf classes.jar Foo.class Bar.class
Synonym to the above
jar tvf classes.jar
Verbosely list all contents of classes.jar
this is necessary for packages to operate correctly
com.newardassociates.Foo
must be classfile Foo.class
in com/newardassociates
inside JAR
package root must start from JAR filesystem root
META-INF
directory is "special" for metadata and related resources
META-INF/MANIFEST.MF
is a "manifest" file describing the JAR file
a collection of "Name: Value" pairs, one per line
https://docs.oracle.com/en/java/javase/17/docs/specs/jar/jar.html#jar-manifest
Names are specific to trigger important functionality
include a manifest file in the creation/update via -m=
FILE or --mainfest=
FILE
specifically do not create a manifest via -M
or --no-manifest
Manifest-Version
: the version of the manifest file
Class-Path
: relative URLs of dependent libraries
Main-Class
: the classname whose main
to invoke when run with java -jar
FILE.jar
can also specify via -e=
CLASSNAME or --main-class=
CLASSNAME
Launcher-Agent-Class
: classname of the java agent to launch before execution
unrecognized attributes are ignored by the JVM
JAR files can support multiple major versions of Java platform releases
set Multi-Release
manifest to true
each versioned resource is in its own JAR directory: META-INF/versions/
N
public API must be exactly identical across versions
a JAR is a "modular JAR" when it has 1+ module descriptor(s) within it
aka module-info.class
in a non-META-INF directory path
this allows for the following CLI parameters:
--module-version
: Defines the
--hash-modules
--module-path
send diagnostic commands to local JVM process
issues a diagnostic command
including a complete list of available commands
Synopsis
$ jcmd -l $ jcmd pid|main-class PerfCounter.print $ jcmd pid|main-class -f filename $ jcmd pid|main-class command [arguments]
PerfCounter.print
prints all the performance counters available
help
prints all the commands available on target
JFR.stop | JFR.start | JFR.dump
Java Flight Recorder start/stop/dump
Thread.print
dump thread stack trace
VM.flags | VM.command_line | VM.system_properties
list VM flags/command-line/system properties
GC.heap_dump filename=filename
create heap dump file
GC.class_histogram filename=filename
create object histogram
generates thread/stack trace
local process
core file
remote debug server
on Windows, requires additional components
dbgeng.dll (Debugging Tools for Windows)
PATH needs to include Hotspot DLL (client.dll | server.dll)
officially experimental and unsupported
jstack
options pid
-l
long listing, including information about java.util.concurrent locks
https://visualvm.github.io
GUI monitoring tool for JVM processes
includes lightweight profiling capabilities
successor to JConsole
open-source
supports plugins
well-documented
Install
brew install visualvm
(or some other package manager)
or download VisualVM and run it
use Applications window to select Java process
even VisualVM itself!
Overview
general view of the JVM settings
Monitor
tracking CPU, Heap, Classes, Threads
can also "Perform GC" and "Heap Dump"
Threads
tracking all threads (live, daemon, finished, etc)
can also "Thread Dump" (stack trace)
Sampler (collecting performance/memory data)
sample CPU
sample Memory
select Tools | Plugins
extensions to VisualVM functionality
OQL queries for heap-object queries, BTrace, etc
downloadable directly from VisualVM
open API
see VisualVM website docs
Explored a bunch of tools
Maybe learned a few new tricks?