Java gets the current process ID and the process ID of all Java processes

Keywords: Java

First, get the java process ID of the current Java operation, which is common on the Internet, that is, the Java program itself prints out the process ID:

package com.test;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class Target {

    public static void main(String[] args) throws InterruptedException {
        System.out.println(getProcessID());
        while(true) {
            Thread.sleep(10000);
        }
    }

    public static final int getProcessID() {  
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println(runtimeMXBean.getName());
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0])  
                .intValue();  
    } 

}

ManagementFactory It is a factory class that manages and monitors Java VM at runtime. It can provide many static interfaces for VM management, such as RuntimeMXBean
RuntimeMXBean Is the runtime management interface of Java virtual machine

Get all running Java processes

package com.test;

import java.util.HashSet;
import java.util.Set;

import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;

public class ProcessID {

    public static void main(String[] args) throws Exception {
         // Get monitoring host
         MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
         // Get all active virtual machine collections
         Set<?> vmlist = new HashSet<Object>(local.activeVms());
         // Traversal set, output PID and process name
         for(Object process : vmlist) {
             MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
             // Get class name
             String processname = MonitoredVmUtil.mainClass(vm, true);
             System.out.println(process + " ------> " + processname);
         }
     }
}

Classes such as MonitoredHost are located in ${JAVA_HOME}/lib/tools.jar

_
Operation result:

2752 ------> 
5172 ------> com.test.Target
5308 ------> com.test.ProcessID

Directly find the method corresponding to Java process ID according to the class


package com.test;

import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;

import sun.jvmstat.monitor.MonitorException;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;

public class ProcessID {

    public static void main(String[] args) throws Exception {
         int pid = getProcess(Target.class);
         System.out.println("PID: "+pid);
    }

    public static int getProcess(Class<?> cls) throws MonitorException, URISyntaxException {
        if(cls == null) {
            return -1;
        }

        // Get monitoring host
        MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
        // Get all active virtual machine collections
        Set<?> vmlist = new HashSet<Object>(local.activeVms());
        // Traversal set, output PID and process name
        for(Object process : vmlist) {
            MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
            // Get class name
            String processname = MonitoredVmUtil.mainClass(vm, true);
            if(cls.getName().equals(processname)) {
                return ((Integer)process).intValue();
            }
        }
        return -1;
    }
}

Find the method corresponding to the Java process ID according to the class name


package com.example.demo;

import sun.jvmstat.monitor.*;

import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;

public class ProcessID2 {
    public static void main(String[] args) throws Exception {
        int pid = getProcess("cn.usr.demo.DemoApplication");
        System.out.println("PID: " + pid);
    }

    public static int getProcess(String cls) throws MonitorException, URISyntaxException {
        if (cls == null) {
            return -1;
        }

        // Get monitoring host
        MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
        // Get all active virtual machine collections
        Set<?> vmlist = new HashSet<Object>(local.activeVms());
        // Traversal set, output PID and process name
        for (Object process : vmlist) {
            MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
            // Get class name
            String processname = MonitoredVmUtil.mainClass(vm, true);
            if (cls.equals(processname)) {
                return ((Integer) process).intValue();
            }
        }
        return -1;
    }
}

Posted by moe on Mon, 18 Nov 2019 07:28:09 -0800