'Java Attach API: VirtualMachine.list() returns empty list?
Any time I'm working with the Java Attach API, no valid VirtualMachines are ever returned. I've tried getting iterating through the list returned by VirtualMachine.list(), and any time I think I've obtained the process ID, I try using VirtualMachine.attach() and it gives me a NullPointerException because the attach() method itself never initializes the exception it tries to throw. I'm working on the project via Eclipse and the Windows process of the application itself when it runs is javaw.exe. What am I doing wrong?
Solution 1:[1]
I had similar issue when I was using java.exe from JRE instead of java.exe from JDK.
App executed via eclipse worked well and all VM's was correctly detected through VirtualMachine.list(), but when I tried to execute standalone app with java.exe from JRE, no VMs was detected. Then I noticed that I'm using JDK in eclipse, so I've changed java.exe in my standalone app to java from JDK and everything was working well.
Change Runtime JRE for your eclipse project to JDK and try again. This possibly colud fix your issue.
Solution 2:[2]
The code that does the VM discovery is in tools.jar on Java 8. That is why running from the JDK is needed.
With modular Java you need the "jdk.attach" module, which you can add to your JRE with jlink.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Piotr Kucia |
| Solution 2 | swpalmer |
