Java exec execute(String command)

Here you can find the source of execute(String command)

Description

Executes the specified string command in a separate process.

License

Open Source License

Parameter

Parameter Description
command command to execute.

Exception

Parameter Description
SecurityException If a security manager exists and its checkExec method doesn't allow creation of the subprocess.
IOException If an I/O error occurs.
InterruptedException if the current thread is interrupted by another thread while it is waiting for the process to complete.
NullPointerException If command is null.
IllegalArgumentException If command is empty.

Return

result of executing this command.

Declaration

public static String[] execute(String command) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*//from  w  w  w.  ja  v  a 2s.co  m
 * This file is part of SerialPundit.
 * 
 * Copyright (C) 2014-2016, Rishi Gupta. All rights reserved.
 *
 * The SerialPundit is DUAL LICENSED. It is made available under the terms of the GNU Affero 
 * General Public License (AGPL) v3.0 for non-commercial use and under the terms of a commercial 
 * license for commercial use of this software. 
 * 
 * The SerialPundit is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;

public class Main {
    /**
     * <p>Executes the specified string command in a separate process. Create an array of response strings 
     * obtained as a result of executing the given command and return it to caller.</p>
     * 
     * @param command command to execute.
     * @return result of executing this command.
     * @throws SecurityException If a security manager exists and its checkExec method doesn't allow creation 
     *         of the subprocess.
     * @throws IOException If an I/O error occurs.
     * @throws InterruptedException if the current thread is interrupted by another thread while it is waiting 
     *         for the process to complete.
     * @throws NullPointerException If command is null.
     * @throws IllegalArgumentException If command is empty.
     */
    public static String[] execute(String command) throws IOException, InterruptedException {

        ArrayList<String> list = new ArrayList<String>();

        Process p = Runtime.getRuntime().exec(command);

        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isr);
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            list.add(line);
        }
        reader.close();
        isr.close();
        is.close();

        p.waitFor();
        if (p.exitValue() != 0) {
            return null;
        }

        return list.toArray(new String[list.size()]);
    }

    /**
     * <p>Executes the specified string command in a separate process. Create an array of response strings 
     * obtained as a result of executing the given command and return it to caller.</p>
     * 
     * @param command command to execute.
     * @param inputToCommand data that is sent to command at its stdin input.
     * @return result of executing this command.
     * @throws SecurityException If a security manager exists and its checkExec method doesn't allow creation 
     *         of the subprocess.
     * @throws IOException If an I/O error occurs.
     * @throws InterruptedException if the current thread is interrupted by another thread while it is waiting 
     *         for the process to complete.
     * @throws NullPointerException If command/inputToCommand is null.
     * @throws IllegalArgumentException If command/inputToCommand is empty.
     */
    public static String[] execute(String command, byte[] inputToCommand) throws IOException, InterruptedException {

        ArrayList<String> list = new ArrayList<String>();

        if ((inputToCommand != null) && (inputToCommand.length != 0)) {

            Process p = Runtime.getRuntime().exec(command);

            OutputStream os = null;
            if ((inputToCommand != null) && (inputToCommand.length != 0)) {
                os = p.getOutputStream();
                os.write(inputToCommand);
                os.flush();
            }

            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isr);
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                System.out.println(line);
                list.add(line);
            }
            reader.close();
            isr.close();
            is.close();
            os.close();

            p.waitFor();
            if (p.exitValue() != 0) {
                return null;
            }
        } else {
            throw new NullPointerException("The inputToCommand is null !");
        }

        return list.toArray(new String[list.size()]);
    }
}

Related

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