Java exec execute(String cmd, String input)

Here you can find the source of execute(String cmd, String input)

Description

execute

License

Open Source License

Declaration

public static String[] execute(String cmd, String input) 

Method Source Code


//package com.java2s;
import java.io.*;
import java.util.*;

public class Main {
    public static String[] execute(String cmd, String input) {
        System.out.println("--> " + cmd);

        ArrayList output = new ArrayList();

        Runtime runtime = Runtime.getRuntime();
        try {/*ww w  .  j a va2  s .  com*/
            Process process = runtime.exec(cmd);
            if (input != null) {
                OutputStream os = process.getOutputStream();
                OutputStreamWriter osr = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osr);
                bw.write(input);
                bw.close();
            }
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String line;
            while ((line = br.readLine()) != null) {
                output.add(line);
            }

        } catch (Exception ex) {
            System.out.println(ex);
        }

        String output_array[] = new String[output.size()];
        output.toArray(output_array);
        return output_array;
    }
}

Related

  1. execToString(final String... args)
  2. execute(boolean returnOutput, boolean returnError, boolean throwException, String[] cmd, File dir, String[] env)
  3. execute(final String applicationName, final String workingDirectory)
  4. execute(final String command)
  5. execute(ProcessBuilder builder, String errorMsg)
  6. execute(String command)
  7. execute(String command)
  8. execute(String command)
  9. execute(String command, Process p, String[] output, boolean cmdStatus)