Skip to content

Conversation

nyg
Copy link
Contributor

@nyg nyg commented Feb 4, 2024

Hello,

Since Java 9, there is a warning (illegal reflective access operation) when using the jdk.jconsole module which contains classes like LocalVirtualMachine that are used to list local VMs and attach to them. On later Java version (17?), accessing these classes makes jmxterm crash.

I implemented a Jdk9JavaProcessManager which relies only on the public Attach API (which is actually what LocalVirtualMachine appears to use under the hood, see: https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java#L123). So my code is voluntarily based on what goes on in LocalVirtualMachine (especially when listing VMs).

I had to adapt WeakCastUtils to build the methodMap using not the from instance (which in Jdk9 case is an instance of VirtualMachineImpl which is not part of the public API) but using the "original" interface, i.e. VirtualMachine. When this distinction is not needed, I just use from.getClass().

The source and target version remain Java 8.

Also, as the mininum Java version is 8, I'm not sure it's useful to keep Jdk5JavaProcessManager.

If WeakCastUtils doesn't serve any other purpose than supporting multiple Java version, probably it could be replaced with Java 9 multi-release JARs: https://nipafx.dev/multi-release-jars-multiple-java-versions/.

Note 1: this PR includes my other PR (#112).

Note 2: as I answered in #48, making jmxterm work with Java 17+ can be done by allowing access to the jdk.jconsole module (with --add-exports jdk.jconsole/...) but this is not supported by Java 8 and cannot be used with the Java 9 --release option.

Best,
nyg

Fixes #48
Fixes #87

EDIT: I tested in the following way: compiled and ran (running jvms and open) jmxterm with both JDK8 and JDK21, but always leaving the maven.compiler.source and target to 1.8.

@nyg nyg mentioned this pull request Apr 3, 2024
LeMyst added a commit to LeMyst/jmxterm that referenced this pull request Jun 19, 2024
LeMyst added a commit to LeMyst/jmxterm that referenced this pull request Jun 19, 2024
LeMyst added a commit to LeMyst/jmxterm that referenced this pull request Jan 24, 2025
@jiaqi jiaqi merged commit d51fbde into jiaqi:main May 14, 2025
@nyg nyg deleted the fix_jdk21_run branch May 14, 2025 17:02
Object vm = staticVirtualMachine.attach(vmd.id());
try {
Class<?> originalVirtualMachine = Class.forName(VirtualMachine.ORIGINAL_CLASS_NAME);
VirtualMachine vmProxy = WeakCastUtils.cast(originalVirtualMachine, VirtualMachine.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to pass vm to cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed I did! I created a new PR to fix that: #129.

@jluehe
Copy link
Contributor

jluehe commented Aug 11, 2025

@jiaqi @nyg, have you ever seen this exception when running with JDK 21:

Exception in thread "main" java.lang.RuntimeException: Can't cast sun.tools.attach.AttachProviderImpl@272113c4: 4089615 to VirtualMachineDescriptor
        at org.cyclopsgroup.jmxterm.jdk9.Jdk9JavaProcess.startManagementAgent(Jdk9JavaProcess.java:55)
        at org.cyclopsgroup.jmxterm.SyntaxUtils.getUrl(SyntaxUtils.java:46)
        at org.cyclopsgroup.jmxterm.boot.CliMain.execute(CliMain.java:135)
        at org.cyclopsgroup.jmxterm.boot.CliMain.main(CliMain.java:41)
Caused by: java.lang.NoSuchMethodException: java.lang.Class.startLocalManagementAgent()
        at java.base/java.lang.Class.getMethod(Class.java:2395)
        at org.cyclopsgroup.jmxterm.utils.WeakCastUtils.cast(WeakCastUtils.java:43)
        at org.cyclopsgroup.jmxterm.utils.WeakCastUtils.cast(WeakCastUtils.java:116)
        at org.cyclopsgroup.jmxterm.utils.WeakCastUtils.cast(WeakCastUtils.java:81)
        at org.cyclopsgroup.jmxterm.jdk9.Jdk9JavaProcess.startManagementAgent(Jdk9JavaProcess.java:52)
        ... 3 more

@nyg nyg mentioned this pull request Sep 7, 2025
nyg pushed a commit to nyg/jmxterm that referenced this pull request Sep 7, 2025
Bumps [org.jline:jline](https://github.com/jline/jline3) from 3.29.0 to 3.30.0.
- [Release notes](https://github.com/jline/jline3/releases)
- [Commits](jline/jline3@jline-3.29.0...jline-3.30.0)

---
updated-dependencies:
- dependency-name: org.jline:jline
  dependency-version: 3.30.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

WARNING: An illegal reflective access operation has occurred Does not build or run with java9

4 participants