Java exec execShell(String cmd)

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

Description

exec Shell

License

Open Source License

Declaration

public static Process execShell(String cmd) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*// w  ww.ja va 2 s. co  m
 * Newisys-Utils - Newisys Utility Classes
 * Copyright (C) 2005 Newisys, Inc. or its licensors, as applicable.
 *
 * Licensed under the Open Software License version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You should
 * have received a copy of the License along with this software; if not, you
 * may obtain a copy of the License at
 *
 * http://opensource.org/licenses/osl-2.0.php
 *
 * This software 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;

public class Main {
    public static Process execShell(String cmd) throws IOException, InterruptedException {
        return Runtime.getRuntime().exec(getShellArgs(cmd));
    }

    public static String[] getShellArgs(String cmd) {
        String osName = System.getProperty("os.name");
        String[] args = new String[3];
        if (osName.startsWith("Windows 9")) {
            args[0] = "command.com";
            args[1] = "/C";
        } else if (osName.startsWith("Windows")) {
            args[0] = "cmd.exe";
            args[1] = "/C";
        } else {
            args[0] = "/bin/sh";
            args[1] = "-c";
        }
        args[2] = cmd;
        return args;
    }
}

Related

  1. execPrint(final boolean out, final String... command)
  2. execProcess(String process)
  3. execProcess(String[] cmdline, final long timeout)
  4. Execption2Strings(boolean rep, Throwable... execptions)
  5. execScript(File shellScriptFile, String[] scriptCommand, PrintWriter execLog)
  6. execShell(String shell)
  7. execSudoCommand(final String _sudoCmd, final String _pw)
  8. execSystemCommand(String[] commands, String executionPath)
  9. execToString(final String... args)