How to get the pid of a Java subprocess? [duplicat

2019-09-14 16:09发布

问题:

This question already has an answer here:

  • How to get PID of process I've just started within java program? 16 answers

If we use Runtime.exec to create a new subprocess:

Process exec(String command) Executes the specified string command in a separate process.

How to get the value of the created process?

回答1:

Until Java 8 (included), you have to use workarounds.

From Java 9 onwards, there is a new getPid method in the Process class.