Java exec executeCommand(String cmd)

Here you can find the source of executeCommand(String cmd)

Description

Execute the specified shell command and wait for it to finish.

License

Open Source License

Parameter

Parameter Description
cmd Shell command string to execute.

Return

true if the return code is 0, false for anything else.

Declaration

public static boolean executeCommand(String cmd) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w w  w .  j  a va 2  s . c  o  m
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import java.io.*;

public class Main {
    /**
     * Execute the specified shell command and wait for it to finish. Return true if the return code is 0, false for
     * anything else. OUT ane ERR are redirected to normal System.out and System.err.
     * 
     * @param cmd Shell command string to execute.
     * @return true if the return code is 0, false for anything else.
     */
    public static boolean executeCommand(String cmd) {
        return executeCommand(cmd, null, null);
    }

    /**
     * Execute the specified shell command with the given environment variables, and within the given work directory.
     * Wait for it to finish. Return true if the return code is 0, false for anything else. OUT and ERR are redirected to
     * normal System.out and System.err.
     * 
     * If env is null, the subprocess inherits the environment settings of the current process. If workDir is null the
     * subprocess will inherit the working directory of the current process.
     * 
     * @param cmd Shell command string to execute.
     * @param env Array of strings, each element of which has environment variable settings in format name=value.
     * @param workDir The working directory of the subprocess.
     * @return true if the return code is 0, false for anything else.
     */
    public static boolean executeCommand(String cmd, String[] env,
            File workDir) {
        try {
            Runtime rt = Runtime.getRuntime();
            final Process pc = rt.exec(cmd, env, workDir);

            // intercept the out
            Thread to = new Thread() {
                public void run() {
                    InputStream cin = pc.getInputStream();
                    try {
                        int nBuf = 0;
                        byte[] buf = new byte[256];
                        while ((nBuf = cin.read(buf)) > 0) {
                            String z = new String(buf, 0, nBuf, "UTF8");
                            System.out.print(z);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            to.start();

            // intercept the err
            Thread te = new Thread() {
                public void run() {
                    InputStream cin = pc.getErrorStream();
                    try {
                        int nBuf = 0;
                        byte[] buf = new byte[256];
                        while ((nBuf = cin.read(buf)) > 0) {
                            String z = new String(buf, 0, nBuf, "UTF8");
                            System.err.print(z);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            te.start();

            int retCode = pc.waitFor();
            if (retCode != 0) {
                System.err.println("Execute '" + cmd + "' failed!");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. executeCommand(final ProcessBuilder pb)
  2. executeCommand(final String cmd)
  3. executeCommand(List cmdArray)
  4. executeCommand(List command)
  5. executeCommand(String cmd)
  6. executeCommand(String comand)
  7. executeCommand(String command)
  8. executeCommand(String command)
  9. executeCommand(String command)