Java Shell Command runCommand(String cmd)

Here you can find the source of runCommand(String cmd)

Description

Run the specified command and return the output as a string.

License

Open Source License

Declaration

protected static String runCommand(String cmd) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**/*from   w  w w  .  j  av a 2 s .co  m*/
     * Run the specified command and return the output as a string.
     */
    protected static String runCommand(String cmd) {
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            BufferedReader cin = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder buffer = new StringBuilder();

            String line = "";
            while (line != null) {
                buffer.append(line);
                line = cin.readLine();
            }
            cin.close();

            return buffer.toString();
        } catch (IOException e) {
            // don't want to log anything for the client to know what we are doing.
            // This will almost always be thrown/caught when the cmd isn't there.
            return null;
        }
    }
}

Related

  1. getJavaVersion(String command)
  2. runCommand(final ProcessBuilder command)
  3. runCommand(String cmd)
  4. runCommand(String command)
  5. runCommand(String command)
  6. runCommand(String command, String args, String file)
  7. runCommand(String command[])