Skip to content

fix: improve vmoption command compatibility with JDK 21#3162

Open
daguimu wants to merge 2 commits intoalibaba:masterfrom
daguimu:fix/vmoption-jdk21-3081
Open

fix: improve vmoption command compatibility with JDK 21#3162
daguimu wants to merge 2 commits intoalibaba:masterfrom
daguimu:fix/vmoption-jdk21-3081

Conversation

@daguimu
Copy link
Copy Markdown
Contributor

@daguimu daguimu commented Mar 24, 2026

Problem

On JDK 21, the vmoption command fails with:

Error during setting vm option: sun.management.Flag

This is caused by JDK module system restrictions in JDK 9+ where internal sun.management APIs are no longer freely accessible to unnamed modules. Users reported that adding --add-opens java.management/sun.management=ALL-UNNAMED doesn't always resolve it.

Fix

  1. JMX proxy fallback: Extract getHotSpotDiagnosticMXBean() helper that first tries ManagementFactory.getPlatformMXBean(), then falls back to JMX.newMXBeanProxy() via MBeanServer. The proxy approach uses standard JMX serialization which avoids the internal sun.management class loading issue.

  2. Better error message: When sun.management access fails, the error now includes an actionable hint suggesting --add-opens java.management/sun.management=ALL-UNNAMED.

  3. Robust complete(): Wrap tab-completion in try/catch to prevent errors when diagnostic options are inaccessible.

Fixes #3081

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 24, 2026

CLA assistant check
All committers have signed the CLA.

@daguimu daguimu force-pushed the fix/vmoption-jdk21-3081 branch from f67a305 to bdfca5a Compare March 24, 2026 20:08
On JDK 21, vmoption command fails with 'Error during setting vm option:
sun.management.Flag' due to JDK module system restrictions on internal
management APIs introduced in JDK 9+.

Changes:
- Extract getHotSpotDiagnosticMXBean() helper that tries the direct
  ManagementFactory.getPlatformMXBean() path first, then falls back to
  JMX.newMXBeanProxy() via MBeanServer, which has better module
  compatibility on JDK 9+
- Improve error message to include actionable hint when sun.management
  access fails, suggesting --add-opens java.management/sun.management=ALL-UNNAMED
- Wrap complete() method in try/catch to handle errors gracefully

Fixes alibaba#3081
@daguimu daguimu force-pushed the fix/vmoption-jdk21-3081 branch from bdfca5a to 63b2fcd Compare March 24, 2026 20:11
…ommand

Log the reason when ManagementFactory.getPlatformMXBean() fails before
falling back to MBeanServer proxy, to aid debugging on JDK 9+ environments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hengyunabc
Copy link
Copy Markdown
Collaborator

实际测试 jdk 21/25 都能正常执行

$ vmoption PrintConcurrentLocks true
Successfully updated the vm option.
 NAME                  BEFORE-VALUE  AFTER-VALUE
-------------------------------------------------
 PrintConcurrentLocks  false         true

具体执行什么命令出错?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用arthas监控JDK21进程的时候,执行vmoption命令,报错Error during setting vm option: sun.management.Flag

3 participants