How to get the process id (PID) of the running Java process
Java 8 and before:
…from Java 9 on:
public static void main(String[] args) throws Exception{ Process proc = Runtime.getRuntime().exec(new String[]{ "/bin/sh", "-c", "echo $PPID" }); if (proc.waitFor() == 0) { InputStream in = proc.getInputStream(); int available = in.available(); byte[] outputBytes = new byte[available]; in.read(outputBytes); String pid = new String(outputBytes); System.out.println("Your pid is " + pid); }}
System.out.println("Your pid is " + Process.getCurrentPid());
Comments
Post a Comment