Java 获取进程号

Java获取进程号,代码如下: 

package com.zlk.test3;

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

public class Test3 {

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

 

你可能感兴趣的:(Java)