import java.lang.management.ManagementFactory;
/**
* 适用Windows(其他系统未经测试)
*/
public class PID
{
public static void main(String[] args)
{
// 在windows上,获取到得name格式为 1234@userName
// 1234为PID,@为分隔符,userName为当前用户
String pid = ManagementFactory.getRuntimeMXBean().getName();
int indexOf = pid.indexOf('@');
if (indexOf > 0)
{
pid = pid.substring(0, indexOf);
}
System.out.println("当前JVM Process ID: " + pid);
}
}
转载自:http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id
本文提供了一种在Windows系统下使用Java代码获取当前运行进程ID(PID)的方法。通过调用ManagementFactory.getRuntimeMXBean().getName()方法并解析返回字符串来实现。
1796

被折叠的 条评论
为什么被折叠?



