I need to recreate the functionalities of the jps tool programmatically. I need to find out all Java running processes along with their id so I can attach to that process (similar to what JConsole does).
I thought the VirtualMachine API would help, but did not get expected result when I run the following
public class ProcessList {
public static void main(String[] args){
List<VirtualMachineDescriptor> vms = VirtualMachine.list();
for(VirtualMachineDescriptor vm : vms){
System.out.println (vm.id());
}
}
}
When I run the code above, it returns just one ID, but when I run jps on the same machine I see several other processes.
The jps command lists the instrumented Java HotSpot VMs on the target system. The command is limited to reporting information on JVMs for which it has the access permissions. Note: JDK 10 added support for using the Attach API when attaching to Java processes running in a separate docker process.
DESCRIPTION. The jps tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions. If jps is run without specifying a hostid, it will look for instrumented JVMs on the local host.
When you run Hadoop on any machine, you can look at the specific processes of Hadoop through one of the utilities provided by Java called the JPS (Java Virtual Machine Process Status) tool.
Go to Control Panel -> System -> Advanced System Settings -> Environment Variables . Add a new path variable called JAVA_HOME : JAVA_HOME=C:\Program Files\Java\jdk1.
jps
uses an internal class - MonitoredHost
of the Oracle/Sun JRE. The activeVMs()
method is used to obtain the list of all active VMs on a host. You can refer to the source of the sun.tools.jps.Jps
class of OpenJDK, to find out how the jps
tool works under the hood.
This is the correct API, ultimately 'MonitoredHost#activeVMs()' and 'VirtualMachine.list()' use the same discovery code via jstat technology. Do you run jps on the command line as a different user? In that case, you would see different JVMs.
See here how JPS is implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With