Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which API does Java's jps tool use internally?

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.

like image 689
vladimir.vivien Avatar asked Oct 10 '11 13:10

vladimir.vivien


People also ask

What is the functionality of JPS command?

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.

What is JPS tool?

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.

What is JPS command in Hadoop?

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.

How do you run JPS?

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.


2 Answers

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.

like image 145
Vineet Reynolds Avatar answered Oct 29 '22 20:10

Vineet Reynolds


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.

like image 27
Ingo Kegel Avatar answered Oct 29 '22 20:10

Ingo Kegel