Get subprocess id in Java

前端 未结 5 896
悲&欢浪女
悲&欢浪女 2020-12-06 05:55

I\'m creating subprocesses in this way:

String command = new String(\"some_program\");

Process p = Runtime.getRuntime().exec(command);

How

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 06:10

    Well there is no documented way to do this, but it happens that the Process implementation class is UNIXProcess, and it has a pid field. So you could use reflection to access this private field to get the ID. Googling around you will find other tricks of calling another shell to get ps output and that kind of thing. Nothing easy.

提交回复
热议问题