Java exec executeCommandLineReturnAll( final String[] command)

Here you can find the source of executeCommandLineReturnAll( final String[] command)

Description

execute Command Line Return All

License

Open Source License

Declaration

public static String[] executeCommandLineReturnAll(
            final String[] command) 

Method Source Code

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

import java.util.ArrayList;

import java.io.InputStreamReader;

import java.io.BufferedReader;

public class Main {
    public static String[] executeCommandLineReturnAll(
            final String[] command) {
        final ArrayList<String> al = new ArrayList<String>();
        try {/*from w  w w .  j  a v a2s  . co  m*/
            final Runtime rt = Runtime.getRuntime();
            rt.traceInstructions(true);
            rt.traceMethodCalls(true);
            final Process p = rt.exec(command);
            final BufferedReader data = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            final BufferedReader error = new BufferedReader(
                    new InputStreamReader(p.getErrorStream()));
            String line;
            while ((line = data.readLine()) != null) {
                al.add(line);
            }
            while ((line = error.readLine()) != null) {
                al.add(line);
            }
            data.close();
            error.close();
        } catch (Exception e) {
            System.out.println("Problem executing -> "
                    + stringArrayToString(command, " ") + " "
                    + e.getLocalizedMessage());
            e.printStackTrace();
            return null;
        }
        final String[] res = new String[al.size()];
        al.toArray(res);
        return res;
    }

    public static String stringArrayToString(final String[] s,
            final String separator) {
        if (s == null) {
            return "";
        }
        final int len = s.length;
        if (len == 1) {
            return s[0];
        }
        if (len == 0) {
            return "";
        }
        final StringBuffer sb = new StringBuffer(s[0]);
        for (int i = 1; i < len; ++i) {
            sb.append(separator);
            sb.append(s[i]);
        }
        return sb.toString();
    }
}

Related

  1. executeCommand1(boolean isProcessBuilder, String command)
  2. executeCommand2(boolean isProcessBuilder, String command)
  3. executeCommandAndExtractStdOut(String cmd)
  4. executeCommandForceDir(String baseCommand, String osPath, File file)
  5. executeCommandLine(String command)
  6. executeCommandLinux(final String _command)
  7. executeDELETE(String parameters)
  8. executeDotCommand(final File dotFile)
  9. executeGET(DataInputStream sockInp, DataOutputStream sockOutp)