Java exec execCommand(String[] cmd)

Here you can find the source of execCommand(String[] cmd)

Description

exec Command

License

Open Source License

Declaration

public static String execCommand(String[] cmd) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String execCommand(String[] cmd) throws Exception {
        Runtime run = Runtime.getRuntime();
        Process pr = null;/*from  w w  w .j a v  a  2 s.  c  o m*/
        try {
            pr = run.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        try {
            pr.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw e;
        }
        BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        StringBuffer out = new StringBuffer();
        try {
            while ((line = buf.readLine()) != null) {
                out.append(line);
                out.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return out.toString();
    }
}

Related

  1. execCmdWindows(String cmd)
  2. execCommand(String cmd)
  3. execCommand(String command)
  4. execCommand(String command)
  5. execCommand(String[] cmd)
  6. execCommandLineUtility(String cmd)
  7. execGenericCommand(String[] command, String redirectOutput)
  8. execGetOutput(String[] command, String[] env)
  9. execHostName(String execCommand)