Example usage for java.lang Process waitFor

List of usage examples for java.lang Process waitFor

Introduction

In this page you can find the example usage for java.lang Process waitFor.

Prototype

public abstract int waitFor() throws InterruptedException;

Source Link

Document

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.

Usage

From source file:edu.ku.brc.af.ui.ProcessListUtil.java

/**
 * @param processId//from www . j ava  2s. c  om
 * @return
 */
public static boolean killProcess(final int processId) {
    String cmd;
    if (UIHelper.isWindows()) {
        cmd = "taskkill /PID " + processId;
    } else {
        cmd = "kill " + processId;
    }

    try {
        Process process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
        return process.exitValue() == 0;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void execCmd(String cmd) {
    DataOutputStream dos = null;//from  w  ww  .  j a v  a  2 s .  c om
    DataInputStream dis = null;
    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        cmd += "\n";
        dos.writeBytes(cmd);
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        p.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dos != null)
                dos.close();
            if (dis != null)
                dis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Process changeBuildProperty(Context c, String propName, String newValue) {
    Process p = null;
    try {/*from   w  w  w. j ava2  s  . c  om*/
        remountSystem(c);

        p = runSuCommandAsync(c, "busybox sed -i \"s/" + propName + "=.*/" + propName + "=" + newValue
                + "/g\" /system/build.prop");
        p.waitFor();
    } catch (Exception d) {
        Log.e("Helper", "Failed to change build.prop. errcode:" + d.toString());
    }
    return p;
}

From source file:com.adguard.compiler.PackageUtils.java

private static void execute(String... commands) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(commands);
    Process p = pb.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;/*  w w  w  .ja v a2  s .  c  o m*/
    while ((line = reader.readLine()) != null) {
        log.debug(line);
    }
    p.waitFor();
    if (p.exitValue() != 0) {
        reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((line = reader.readLine()) != null) {
            log.error(line);
        }
        throw new IOException("Command " + ArrayUtils.toString(commands) + " not success");
    }
}

From source file:mitm.common.sms.transport.gnokii.Gnokii.java

public static String identify() throws GnokiiException {
    String[] cmd = new String[] { GNOKII_BIN, "--identify" };

    try {/*from w w  w .j av  a2  s .  c om*/
        ProcessBuilder processBuilder = new ProcessBuilder(cmd);
        processBuilder.redirectErrorStream(true);

        Process process = processBuilder.start();

        String output = IOUtils.toString(process.getInputStream());

        int exitValue;

        try {
            exitValue = process.waitFor();
        } catch (InterruptedException e) {
            throw new GnokiiException("Identify error. Output: " + output);
        }

        if (exitValue != 0) {
            throw new GnokiiException("Identify error. Output: " + output);
        }

        return output;
    } catch (IOException e) {
        throw new GnokiiException(e);
    }
}

From source file:com.moss.posixfifosockets.PosixFifoSocket.java

public static void createFifo(File path) throws IOException {
    if (path.exists()) {
        throw new IOException("File already exists: " + path.getAbsolutePath());
    }//  w ww.  ja  va2s .  c  om
    if (!path.getParentFile().exists()) {
        throw new FileNotFoundException(path.getParent());
    }
    try {
        if (path.exists()) {
            throw new RuntimeException("Path really does exist: " + path.getAbsolutePath());
        }

        final Process p = Runtime.getRuntime().exec("mkfifo " + path.getAbsolutePath());
        int result = p.waitFor();
        if (result != 0) {

            String stdOut = read(p.getInputStream());
            String stdErr = read(p.getErrorStream());
            throw new IOException("Error creating fifo at " + path.getAbsolutePath() + ": Received error code "
                    + result + ", STDOUT: " + stdOut + ", STDERR: " + stdErr);
        } else if (!path.exists()) {
            throw new RuntimeException("mkfifo didn't do its job: " + path.getAbsolutePath());
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:bizlogic.Command.java

public static int Process(String _command, Connection con, PrintWriter out) throws SQLException {

    try {//from ww  w  .  j a va  2s .  co  m
        System.out.print("Processing command: ");
        System.out.println(_command);

        String string = _command;
        String[] _array = string.split(",");

        String command = _array[0];

        switch (command) {
        case "addSensor":
            bizlogic.Sensors.add(con, _array[1], _array[2], _array[3], _array[4], _array[5], _array[6]);
            bizlogic.Sensors.list(con);
            break;

        case "delSensor":
            bizlogic.Sensors.del(con, _array[1]);
            bizlogic.Sensors.list(con);
            break;

        case "listSensors":
            bizlogic.Sensors.list(con);
            out.println("sensors.json ok");
            break;

        case "addRecord":
            bizlogic.Records.add(con, _array[1], _array[2], _array[3], _array[4]);
            bizlogic.Records.list(con);
            break;

        case "delRecord":
            bizlogic.Records.del(con, _array[1]);
            bizlogic.Records.list(con);
            break;

        case "startRecords":
            bizlogic.Records.setColumn(con, _array[1], "USERCONF.LOG_LIST", "RUNNING", "true");
            // (conDB, records, column, value)
            //bizlogic.Records.list(con);
            out.println("startRecords " + _array[1]);
            break;

        case "stopRecords":
            bizlogic.Records.setColumn(con, _array[1], "USERCONF.LOG_LIST", "RUNNING", "false");
            // (conDB, records, column, value)
            //bizlogic.Records.list(con);
            out.println("stopRecords " + _array[1]);
            break;

        case "listRecords":
            bizlogic.Records.list(con);
            out.println("listRecords ok");
            break;

        case "writeCSV":
            bizlogic.Records.writeCSV(con, _array[1]);
            out.println("writeCSV " + _array[1] + " Value" + " Time");
            break;

        case "setConf":
            String[] confString = new String[] { "date", "+%D", "--set", _array[3] + " " + _array[2] };
            System.out.println("confString=" + Arrays.toString(confString));
            Process p = java.lang.Runtime.getRuntime().exec(confString);
            int setConf_status = p.waitFor();
            System.out.println("confStatus = " + Integer.toString(setConf_status));
            if (setConf_status != -1) {
                out.println("setConf " + "ok");
            } else {
                out.println("ERROR Date not set");
            }
            break;

        case "serverTime":
            DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            out.println("Server time: " + sdf.format(date));
            break;

        case "changeIP":

            if (bizlogic.modSettings.validateIP(_array[1])) {
                System.out.println("changeIP to " + _array[1]);
                bizlogic.modSettings.resetIP();
                if (!_array[1].equals("0.0.0.0")) {
                    bizlogic.modSettings.setIP(_array[1]);
                }
                out.println("Server rebooting to set the IP");
                out.close();
                con.close();
                java.lang.Runtime.getRuntime().exec("sudo reboot");
            } else {
                out.println("ERROR: Bad IP address." + " Enter a valid IPv4 addess");
                System.out.println("Bad IP: " + _array[1]);
                java.lang.Runtime.getRuntime().exec(
                        "pid2=`ps aux | " + "grep \"[d]l_hwlogic\" | awk '{print $2}'`\n" + "kill -9 $pid2");
            }
            break;

        default:
            break;
        }
        return 1;
    } catch (IOException | ParseException | InterruptedException ex) {
        Logger.getLogger(Command.class.getName()).log(Level.WARNING, null, ex);
        return -1;
    }
}

From source file:citibob.licensor.MakeLauncher.java

static void exec(File dir, String... cmds) throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(cmds, null, dir);
    InputStream in = proc.getInputStream();
    int c;//w  w  w . j a v a  2  s .  c  o  m
    while ((c = in.read()) >= 0)
        System.out.write(c);
    proc.waitFor();
    System.out.println("---> exit value = " + proc.exitValue());
}

From source file:Main.java

public static void killProcess(int pid) {
    Process sh = null;
    DataOutputStream os = null;//from  w  w  w . java 2 s .co  m
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "kill -9 " + pid + "\n";
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void forceStopAPK(String pkgName) {
    Process sh = null;
    DataOutputStream os = null;/* www.j  a  v a 2 s. co  m*/
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "am force-stop" + pkgName;
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}