Java exec exec(String cmd)

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

Description

exec

License

Open Source License

Declaration

public static boolean exec(String cmd) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.File;

import java.io.IOException;

public class Main {
    public static boolean exec(String cmd) {
        try {/*from  w  w w .  ja  v  a 2s. c o  m*/
            String cmds[] = null;
            if (isNT()) {
                // NT
                cmds = new String[3];
                cmds[0] = "cmd";
                cmds[1] = "/c";
                cmds[2] = cmd;
            } else {
                // UNIX
                cmds = new String[3];
                cmds[0] = "/bin/sh";
                cmds[1] = "-c";
                cmds[2] = cmd;
            }
            Process process = Runtime.getRuntime().exec(cmds);
            process.waitFor();

            if (process.exitValue() == 0) {
                /**
                 * pOut = new BufferedReader(
                 * new InputStreamReader(process.getInputStream()));
                 * while ((l = pOut.readLine()) != null) {
                 * System.out.println(l);
                 * }
                 **/
                return true;
            } else {
                /**
                 * pOut = new BufferedReader(
                 * new InputStreamReader(process.getErrorStream()));
                 * l = null;
                 * while ((l = pOut.readLine()) != null) {
                 * System.out.println(l);
                 * }
                 **/
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return false;
    }

    /**
     * Checks if this is NT.
     */
    public static boolean isNT() {
        return File.separator.equals("\\");
    }
}

Related

  1. exec(String cmd)
  2. exec(String cmd)
  3. exec(String cmd)
  4. exec(String cmd)
  5. exec(String cmd)
  6. exec(String cmd, File dir)
  7. exec(String command)
  8. exec(String command)
  9. exec(String command)