Java Utililty Methods exec

List of utility methods to do exec

Description

The list of methods to do exec are organized into topic(s).

Method

ListexecuteCommand(final String cmd)
execute Command
List<String> output = new ArrayList<String>();
Process p;
try {
    p = Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
    return output;
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()), 8 * 1024);
...
StringexecuteCommand(List cmdArray)
Execute a command and return all the output.
ProcessBuilder builder = new ProcessBuilder(cmdArray);
builder.redirectErrorStream(true);
Process process = builder.start();
try (InputStream is = process.getInputStream(); Scanner scanner = new Scanner(is).useDelimiter("$")) {
    String output = scanner.hasNext() ? scanner.next() : "";
    process.waitFor();
    int exitValue = process.exitValue();
    if (exitValue != 0) {
...
voidexecuteCommand(List command)
execute Command
ProcessBuilder pb = new ProcessBuilder();
pb.command(command);
System.out.println("Executing: " + pb.command());
Process process = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
...
StringexecuteCommand(String cmd)
execute Command
Process p;
try {
    p = Runtime.getRuntime().exec(cmd);
    InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
    StringBuffer buffer = new StringBuffer();
    for (;;) {
        int c = stdoutStream.read();
        if (c == -1) {
...
booleanexecuteCommand(String cmd)
Execute the specified shell command and wait for it to finish.
return executeCommand(cmd, null, null);
voidexecuteCommand(String comand)
execute Command
Process p;
try {
    p = Runtime.getRuntime().exec(comand);
    p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuffer sb = new StringBuffer();
    String line = reader.readLine();
    sb.append(line);
...
StringexecuteCommand(String command)
execute Command
try {
    Runtime r = Runtime.getRuntime();
    String[] cmd = { "/bin/sh", "-c", command };
    Process p = r.exec(cmd);
    p.waitFor();
    return inputStreamToString(p.getInputStream());
} catch (IOException ex) {
    return ex.toString();
...
voidexecuteCommand(String command)
execute command using system.getRuntime method
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
p.destroy();
StringexecuteCommand(String command)
execute Command
Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", command });
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = input.readLine()) != null) {
    output.append(line);
    output.append("\n");
input.close();
return output.toString();
StringexecuteCommand(String command)
execute Command
return executeCommand(command, "./");