Here you can find the source of execute(String command)
Executes the specified string command in a separate process.
Parameter | Description |
---|---|
command | command to execute. |
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. |
public static String[] execute(String command) throws IOException, InterruptedException
//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()]); } }