ProcessUtil { public static int getPID(final Process process) throws ProcessIDNotRetrievableException { Integer pid; try { if (getOS() == EOperatingSystem.LINUX || getOS() == EOperatingSystem.MAC) { Field f = process.getClass().getDeclaredField("pid"); f.setAccessible(true); pid = f.getInt(process); return pid; } else if (getOS() == EOperatingSystem.WIN) { return WinUtils.getWindowsProcessId(process).intValue(); } } catch (Exception e) { throw new ProcessIDNotRetrievableException("Could not retrieve process ID", e); } throw new UnsupportedOperationException(); } private  ProcessUtil(); static EOperatingSystem getOS(); static Process getProcessListProcess(); static Collection<ProcessInfo> getRunningJavaProcesses(); static int getPID(final Process process); static void killProcess(final int pid); static void killProcess(final Process process);  }
@Test public void testGetPID() throws ProcessIDNotRetrievableException, IOException { Process p = new ProcessBuilder("ping", "127.0.0.1", "-n 3").start(); int pid = ProcessUtil.getPID(p); assertTrue("Invalid process ID obtained", pid > 1); p.destroyForcibly(); }