首页 > 代码库 > java 获取进程的processId

java 获取进程的processId

 

package us.mine.demo.jvm.util;

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

public class GetPID {

    public static void main(String[] args){
        System.out.println(getPid());
    }
    
    public static int getPid() {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();        
        String name = runtime.getName(); // format: "pid@hostname"
        try {
            return Integer.parseInt(name.substring(0, name.indexOf(‘@‘)));
        } catch (Exception e) {
            return -1;
        }
    }  
}

 

java 获取进程的processId