@Test public void execCommand() throws Exception { String testString = "alluxio"; String[] cmd = new String[]{"bash", "-c", "echo " + testString}; String result = new ShellCommand(cmd).run(); assertEquals(testString + "\n", result); }
public String run() throws IOException { Process process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); try { StringBuilder output = new StringBuilder(); String line = inReader.readLine(); while (line != null) { output.append(line); output.append("\n"); line = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new ShellUtils.ExitCodeException(exitCode, output.toString()); } return output.toString(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { try { InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException e) { LOG.warn(String.format("Error while closing the input stream of process %s: %s", process, e.getMessage())); } process.destroy(); } }
ShellCommand { public String run() throws IOException { Process process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); try { StringBuilder output = new StringBuilder(); String line = inReader.readLine(); while (line != null) { output.append(line); output.append("\n"); line = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new ShellUtils.ExitCodeException(exitCode, output.toString()); } return output.toString(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { try { InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException e) { LOG.warn(String.format("Error while closing the input stream of process %s: %s", process, e.getMessage())); } process.destroy(); } } }
ShellCommand { public String run() throws IOException { Process process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); try { StringBuilder output = new StringBuilder(); String line = inReader.readLine(); while (line != null) { output.append(line); output.append("\n"); line = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new ShellUtils.ExitCodeException(exitCode, output.toString()); } return output.toString(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { try { InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException e) { LOG.warn(String.format("Error while closing the input stream of process %s: %s", process, e.getMessage())); } process.destroy(); } } ShellCommand(String[] execString); }
ShellCommand { public String run() throws IOException { Process process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); try { StringBuilder output = new StringBuilder(); String line = inReader.readLine(); while (line != null) { output.append(line); output.append("\n"); line = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new ShellUtils.ExitCodeException(exitCode, output.toString()); } return output.toString(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { try { InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException e) { LOG.warn(String.format("Error while closing the input stream of process %s: %s", process, e.getMessage())); } process.destroy(); } } ShellCommand(String[] execString); String run(); CommandReturn runWithOutput(); String toString(); String[] getCommand(); }
ShellCommand { public String run() throws IOException { Process process = new ProcessBuilder(mCommand).redirectErrorStream(true).start(); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); try { StringBuilder output = new StringBuilder(); String line = inReader.readLine(); while (line != null) { output.append(line); output.append("\n"); line = inReader.readLine(); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new ShellUtils.ExitCodeException(exitCode, output.toString()); } return output.toString(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } finally { try { InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException e) { LOG.warn(String.format("Error while closing the input stream of process %s: %s", process, e.getMessage())); } process.destroy(); } } ShellCommand(String[] execString); String run(); CommandReturn runWithOutput(); String toString(); String[] getCommand();  }