Here you can find the source of exec(String cmd)
public static boolean exec(String cmd)
//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("\\"); } }