Java exec executeShellCommand(String command)

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

Description

execute Shell Command

License

Apache License

Declaration

public static Process executeShellCommand(String command) throws IOException, InterruptedException 

Method Source Code

//package com.java2s;
/**//from   w ww . j a  v a 2s . co m
 * Copyright [2011] [Datasalt Systems S.L.]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    public static Process executeShellCommand(String command) throws IOException, InterruptedException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        process.waitFor();
        if (process.exitValue() != 0) {
            String error = convertStreamToString(process.getErrorStream());
            String output = convertStreamToString(process.getInputStream());
            throw new RuntimeException(output + "\n" + error); //TODO change this for a proper named exception
        }

        return process;
    }

    /**
     * Reads an InputStream and dumps it to a stream
     * @param is
     * @throws IOException
     */
    public static String convertStreamToString(InputStream is) throws IOException {
        /*
        * To convert the InputStream to String we use the
        * Reader.read(char[] buffer) method. We iterate until the
        * Reader return -1 which means there's no more data to
        * read. We use the StringWriter class to produce the string.
        */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new InputStreamReader(is, "UTF-8");
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
}

Related

  1. executeProcessAndGetOutputAsStringList(final String command)
  2. executePUT(DataInputStream sockInp, DataOutputStream sockOutp)
  3. executeServer()
  4. executeShellCmdAndReturn(String cmd)
  5. executeShellCommand(String command)
  6. executeShellCommand(String command, File dir)
  7. executeShellCommand(String commandName)
  8. executeShellScript(final String shellScript, final File tempDirectory)
  9. executeSMTPSend(String fromEmailAddress, List toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments)