How to Use jmxterm to Monitor and Control JVM Applications

jmxterm: A Lightweight CLI for Managing Java MBeans

jmxterm is a compact, command-line tool for interacting with Java Management Extensions (JMX) MBeans. It provides a fast, scriptable way to inspect attributes, invoke operations, and browse MBean hierarchies without needing a GUI like JConsole or VisualVM. This article explains what jmxterm is, when to use it, how to install it, common commands, scripting tips, and practical examples.

What jmxterm is and when to use it

  • Lightweight CLI: A single JAR that runs from the command line with no heavy GUI dependencies.
  • Remote and local access: Connect to local JVMs via JMX connectors or to remote JVMs over network JMX.
  • Automation-friendly: Easy to script for monitoring, troubleshooting, or operational tasks.
  • Good fit when: You need quick inspection or automation in CI/CD, production servers without an X display, or in scripts run by ops engineers.

Installation

  1. Download the jmxterm JAR (commonly named jmxterm-*.jar) from its releases page or a trusted repository.
  2. Place the JAR on a machine with Java 8+ installed.
  3. Run it with:

Code

java -jar jmxterm-.jar

Connecting to a JVM

  • Local attach (using JMX service URL):
  • Using hostname and port:
    • connect host:port
  • With authentication: Use -u username and -p password options or include credentials in the JMX URL if configured.
  • Example:

Code

> open localhost:9010

Browsing MBeans

  • List domains: domains
  • List MBeans in a domain: beans or simply beans to list all registered MBeans.
  • Example:

Code

> domains > beans java.lang

Reading and writing attributes

  • Get an attribute:

Code

get
  • Set an attribute:

Code

set
  • Example:

Code

> get java.lang:type=Memory HeapMemoryUsage > set com.example:type=Config Enabled true

Invoking operations

  • Use invoke to call MBean operations:

Code

invoke [args…]
  • Example:

Code

> invoke com.example:type=Service restart

Useful commands and output formats

  • help — show command help.
  • info — detailed info including attributes and operations.
  • close / quit — end the session.
  • formatting: jmxterm supports different output formats (plain, JSON) depending on build/version; use options to choose machine-readable output for scripts.

Scripting examples

  • Non-interactive mode: pass commands via stdin or a file:

Code

java -jar jmxterm.jar -l localhost:9010 -n -v silent < commands.txt
  • Example commands.txt:

Code

beans get java.lang:type=Memory HeapMemoryUsage quit
  • Use in automation: wrap jmxterm calls in shell scripts to collect metrics or trigger operations as part of deployment or monitoring checks.

Security considerations

  • Ensure JMX is secured when exposed over networks: enable SSL/TLS and authentication.
  • Limit network exposure and use firewall rules or VPNs for remote access.
  • Prefer short-lived credentials or use a bastion host when automating remote operations.

Troubleshooting tips

  • Connection refused: verify the target JVM has JMX enabled and correct host/port.
  • Authentication failures: confirm credentials and JMX access configuration.
  • MBean not found: list beans or domains to confirm exact objectName and case.
  • For remote RMI setups, consider using the same hostname/IP that the JVM advertises (use -Djava.rmi.server.hostname if needed).

Alternatives and when to choose them

  • Use JConsole/VisualVM for graphical exploration and profiling.
  • Use Jolokia (HTTP/JSON JMX bridge) if you need RESTful access and easier integration with web tools.
  • Choose jmxterm when you need lightweight, scriptable, terminal-based access.

Summary

jmxterm is a practical, minimal CLI for working with Java MBeans—ideal for quick inspections, scripting, and operations on headless servers. With basic commands for connecting, listing beans, reading/writing attributes, and invoking operations, it fits neatly into automation workflows and troubleshooting toolkits.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *