Home > Java > javaTutorial > How does Java get the current process ID and the process IDs of all Java processes

How does Java get the current process ID and the process IDs of all Java processes

黄舟
Release: 2017-06-04 09:18:42
Original
3234 people have browsed it

This article mainly introduces how Java obtains the current process ID and the process ID of all Java processes. It has a certain reference value. Interested friends can refer to it.

The first is to obtain the current Java running Java process ID, this 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(); 
  } 

}
Copy after login

Running results:

2896@PC-20150603VRPL
2896
Copy after login

ManagementFactory is a Java VM that manages and monitors Java VM at runtime Factory class, it can provide many staticinterfaces for managing VM, compared with RuntimeMXBean;

RuntimeMXBean is the runtime management interface of Java virtual machine.

The current process ID is 2896.

But this way of letting the Java process print the ID by itself is not easy to use, because many times we need to know the running Java process without modifying the code of another Java program. ID, if you also have this need, you can take a look at the following solution:

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 {
     // 获取监控主机
     MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
     // 取得所有在活动的虚拟机集合
     Set<?> vmlist = new HashSet<Object>(local.activeVms());
     // 遍历集合,输出PID和进程名
     for(Object process : vmlist) {
       MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
       // 获取类名
       String processname = MonitoredVmUtil.mainClass(vm, true);
       System.out.println(process + " ------> " + processname);
     }
   }
}
Copy after login

Run results:

2752 ------> 
5172 ------> com.test.Target
5308 ------> com.test.ProcessID
Copy after login

In this way, you can find the process number based on the class name.

MonitoredHost and other classes are located in ${JAVA_HOME}/lib/tools.jar

The following is a method that can directly find the corresponding Java process ID based on 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;
    }

    // 获取监控主机
    MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
    // 取得所有在活动的虚拟机集合
    Set<?> vmlist = new HashSet<Object>(local.activeVms());
    // 遍历集合,输出PID和进程名
    for(Object process : vmlist) {
      MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
      // 获取类名
      String processname = MonitoredVmUtil.mainClass(vm, true);
      if(cls.getName().equals(processname)) {
        return ((Integer)process).intValue();
      }
    }
    return -1;
  }
}
Copy after login

Run result:

PID: 5172

The source code is located in the com.test package of my Github project aoptracer

The above is the detailed content of How does Java get the current process ID and the process IDs of all Java processes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template