Search code examples
javasubprocess

How to get the pid of a Java subprocess?


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?


Solution

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

    From Java 9 onwards, there is a new pid() method in the Process class.