java获取启动应用程序的PID

       下面这段程序通过java反射获取一个类的私有属性,  学习了! 

       ProcessBuilder builder = new ProcessBuilder();

        builder.directory(new File("/opt/google/chrome"));     //设置应用程序所在的路径
        builder.command("chrome", "--user-data-dir=/opt/temp");      //程序的启动命令


try {
final Process process = builder.start();
Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
try {
        Class clazz = Class.forName("java.lang.UNIXProcess");
Field pidField = clazz.getDeclaredField("pid");
pidField.setAccessible(true);
Object value = pidField.get(process);
System.err.println("pid = " + value);

   } catch (Throwable e) {
e.printStackTrace();
   }
}
},2000);
} catch (IOException e1) {
e1.printStackTrace();
}


       
}

你可能感兴趣的:(java获取启动应用程序的PID)