@Test public void testGetPID() { Assert.assertTrue(OSUtils.isUnix() || OSUtils.isWindows()); Assert.assertTrue(OSUtils.getPID() != -1); }
public static long getPID() { if (isUnix()) { try { final Process process = new ProcessBuilder() .command("/bin/sh", "-c", "echo $PPID") .start(); final int exitCode = process.waitFor(); if (exitCode != 0) { LOG.log(Level.SEVERE, "Unable to determine PID. Exit code = {0}", exitCode); final StringBuilder errorMsg = new StringBuilder(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { for (int i = 0; i < 10 && reader.ready(); ++i) { errorMsg.append(reader.readLine()).append('\n'); } } LOG.log(Level.SEVERE, "Error:\n{0}", errorMsg); return -1; } final byte[] returnBytes = new byte[128]; if (process.getInputStream().read(returnBytes) == -1) { LOG.log(Level.FINE, "No data read because end of stream was reached"); } process.destroy(); return Long.parseLong(new String(returnBytes, StandardCharsets.UTF_8).trim()); } catch (final IOException | InterruptedException e) { LOG.log(Level.SEVERE, "Unable to determine PID", e); return -1; } } else if (isWindows()) { final String name = ManagementFactory.getRuntimeMXBean().getName(); try { return Long.parseLong(name.split("@")[0]); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Unable to parse PID from string " + name, e); return -1; } } else { return -1; } }
OSUtils { public static long getPID() { if (isUnix()) { try { final Process process = new ProcessBuilder() .command("/bin/sh", "-c", "echo $PPID") .start(); final int exitCode = process.waitFor(); if (exitCode != 0) { LOG.log(Level.SEVERE, "Unable to determine PID. Exit code = {0}", exitCode); final StringBuilder errorMsg = new StringBuilder(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { for (int i = 0; i < 10 && reader.ready(); ++i) { errorMsg.append(reader.readLine()).append('\n'); } } LOG.log(Level.SEVERE, "Error:\n{0}", errorMsg); return -1; } final byte[] returnBytes = new byte[128]; if (process.getInputStream().read(returnBytes) == -1) { LOG.log(Level.FINE, "No data read because end of stream was reached"); } process.destroy(); return Long.parseLong(new String(returnBytes, StandardCharsets.UTF_8).trim()); } catch (final IOException | InterruptedException e) { LOG.log(Level.SEVERE, "Unable to determine PID", e); return -1; } } else if (isWindows()) { final String name = ManagementFactory.getRuntimeMXBean().getName(); try { return Long.parseLong(name.split("@")[0]); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Unable to parse PID from string " + name, e); return -1; } } else { return -1; } } }
OSUtils { public static long getPID() { if (isUnix()) { try { final Process process = new ProcessBuilder() .command("/bin/sh", "-c", "echo $PPID") .start(); final int exitCode = process.waitFor(); if (exitCode != 0) { LOG.log(Level.SEVERE, "Unable to determine PID. Exit code = {0}", exitCode); final StringBuilder errorMsg = new StringBuilder(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { for (int i = 0; i < 10 && reader.ready(); ++i) { errorMsg.append(reader.readLine()).append('\n'); } } LOG.log(Level.SEVERE, "Error:\n{0}", errorMsg); return -1; } final byte[] returnBytes = new byte[128]; if (process.getInputStream().read(returnBytes) == -1) { LOG.log(Level.FINE, "No data read because end of stream was reached"); } process.destroy(); return Long.parseLong(new String(returnBytes, StandardCharsets.UTF_8).trim()); } catch (final IOException | InterruptedException e) { LOG.log(Level.SEVERE, "Unable to determine PID", e); return -1; } } else if (isWindows()) { final String name = ManagementFactory.getRuntimeMXBean().getName(); try { return Long.parseLong(name.split("@")[0]); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Unable to parse PID from string " + name, e); return -1; } } else { return -1; } } private  OSUtils(); }
OSUtils { public static long getPID() { if (isUnix()) { try { final Process process = new ProcessBuilder() .command("/bin/sh", "-c", "echo $PPID") .start(); final int exitCode = process.waitFor(); if (exitCode != 0) { LOG.log(Level.SEVERE, "Unable to determine PID. Exit code = {0}", exitCode); final StringBuilder errorMsg = new StringBuilder(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { for (int i = 0; i < 10 && reader.ready(); ++i) { errorMsg.append(reader.readLine()).append('\n'); } } LOG.log(Level.SEVERE, "Error:\n{0}", errorMsg); return -1; } final byte[] returnBytes = new byte[128]; if (process.getInputStream().read(returnBytes) == -1) { LOG.log(Level.FINE, "No data read because end of stream was reached"); } process.destroy(); return Long.parseLong(new String(returnBytes, StandardCharsets.UTF_8).trim()); } catch (final IOException | InterruptedException e) { LOG.log(Level.SEVERE, "Unable to determine PID", e); return -1; } } else if (isWindows()) { final String name = ManagementFactory.getRuntimeMXBean().getName(); try { return Long.parseLong(name.split("@")[0]); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Unable to parse PID from string " + name, e); return -1; } } else { return -1; } } private  OSUtils(); static boolean isWindows(); static boolean isUnix(); static boolean isLinux(); static boolean isMac(); static long getPID(); static void kill(final long pid); static String formatVariable(final String variableName); }
OSUtils { public static long getPID() { if (isUnix()) { try { final Process process = new ProcessBuilder() .command("/bin/sh", "-c", "echo $PPID") .start(); final int exitCode = process.waitFor(); if (exitCode != 0) { LOG.log(Level.SEVERE, "Unable to determine PID. Exit code = {0}", exitCode); final StringBuilder errorMsg = new StringBuilder(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { for (int i = 0; i < 10 && reader.ready(); ++i) { errorMsg.append(reader.readLine()).append('\n'); } } LOG.log(Level.SEVERE, "Error:\n{0}", errorMsg); return -1; } final byte[] returnBytes = new byte[128]; if (process.getInputStream().read(returnBytes) == -1) { LOG.log(Level.FINE, "No data read because end of stream was reached"); } process.destroy(); return Long.parseLong(new String(returnBytes, StandardCharsets.UTF_8).trim()); } catch (final IOException | InterruptedException e) { LOG.log(Level.SEVERE, "Unable to determine PID", e); return -1; } } else if (isWindows()) { final String name = ManagementFactory.getRuntimeMXBean().getName(); try { return Long.parseLong(name.split("@")[0]); } catch (final NumberFormatException e) { LOG.log(Level.SEVERE, "Unable to parse PID from string " + name, e); return -1; } } else { return -1; } } private  OSUtils(); static boolean isWindows(); static boolean isUnix(); static boolean isLinux(); static boolean isMac(); static long getPID(); static void kill(final long pid); static String formatVariable(final String variableName);  }