A practical guide to calling jcmd diagnostic commands programmatically using JMX instead of spawning a new JVM process. Covers attaching to a running JVM via VirtualMachine.attach(), starting the local JMX management agent, connecting via JMXConnectorFactory, and invoking DiagnosticCommand MBean methods. Includes a key detail: jcmd command names must be transformed to Java method naming conventions (e.g., VM.command_line → vmCommandLine) before invoking them on the MBeanServerConnection. Full working code is provided.
Sort: