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:com.ebiznext.flume.source.TestMultiLineExecSource.java

private static List<String> exec(String command) throws Exception {
    String[] commandArgs = command.split("\\s+");
    Process process = new ProcessBuilder(commandArgs).start();
    BufferedReader reader = null;
    try {//from w ww . ja  v a  2  s .c  om
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        List<String> result = Lists.newArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    } finally {
        process.destroy();
        if (reader != null) {
            reader.close();
        }
        int exit = process.waitFor();
        if (exit != 0) {
            throw new IllegalStateException("Command [" + command + "] exited with " + exit);
        }
    }
}

From source file:com.samczsun.helios.Helios.java

public static void relaunchAsAdmin() throws IOException, InterruptedException, URISyntaxException {
    File currentJarLocation = getJarLocation();
    File javawLocation = getJavawLocation();
    if (currentJarLocation == null) {
        SWTUtil.showMessage("Could not relaunch as admin - unable to find Helios.jar");
        return;//www  . ja v  a2s .co  m
    }
    if (javawLocation == null) {
        SWTUtil.showMessage("Could not relaunch as admin - unable to find javaw.exe");
        return;
    }
    File tempVBSFile = File.createTempFile("tmpvbs", ".vbs");
    PrintWriter writer = new PrintWriter(tempVBSFile);
    writer.println("Set objShell = CreateObject(\"Wscript.Shell\")");
    writer.println("strPath = Wscript.ScriptFullName");
    writer.println("Set objFSO = CreateObject(\"Scripting.FileSystemObject\")");
    writer.println("Set objFile = objFSO.GetFile(strPath)");
    writer.println("strFolder = objFSO.GetParentFolderName(objFile)");
    writer.println("Set UAC = CreateObject(\"Shell.Application\")");
    writer.println("UAC.ShellExecute \"\"\"" + javawLocation.getAbsolutePath() + "\"\"\", \"-jar \"\""
            + currentJarLocation.getAbsolutePath() + "\"\"\", strFolder, \"runas\", 1");
    writer.println("WScript.Quit 0");
    writer.close();

    Process process = Runtime.getRuntime().exec("cscript " + tempVBSFile.getAbsolutePath());
    process.waitFor();
    System.exit(process.exitValue());
}

From source file:com.sinpo.xnfc.nfc.Util.java

public static String execRootCmd(String cmd) {
    String result = "";
    DataOutputStream dos = null;// w  ww.j av a 2 s. c o  m
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        String line = null;
        while ((line = dis.readLine()) != null) {
            result += line + "\r\n";
        }
        p.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:com.mewmew.fairy.v1.book.Xargs.java

public static void exec(Map<String, String> env, String cmd[], boolean redirectError, Output<String> output)
        throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(cmd);
    if (env != null) {
        pb.environment().putAll(env);//from w w  w .  j  a  v a  2s. c  o  m
    }
    if (redirectError) {
        pb.redirectErrorStream(true);
    }
    final Process p = pb.start();
    if (!redirectError) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    LineIterator err = new LineIterator(new InputStreamReader(p.getErrorStream()));
                    while (err.hasNext()) {
                        err.next();
                    }
                } finally {
                }
            }
        }).start();
    }
    LineIterator out = new LineIterator(new InputStreamReader(p.getInputStream()));
    while (out.hasNext()) {
        output.output(out.nextLine());
    }
    int code = p.waitFor();
    if (code != 0) {
        throw new RuntimeException(String.format("return != 0, code = %d", code));
    }
}

From source file:com.theonespy.util.Util.java

public static void disableMobileData() {
    Util.Log("Disable mobile data");

    try {//  w w  w .  ja  v  a  2 s .  c  o  m
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        outputStream.writeBytes("svc data disable\n ");
        outputStream.flush();

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String execRootCmd(String cmd) {
    String result = "result : ";
    try {/*from  ww w  .jav a 2s . com*/
        Process p = Runtime.getRuntime().exec("su ");
        OutputStream outStream = p.getOutputStream();
        DataOutputStream dOutStream = new DataOutputStream(outStream);
        InputStream inStream = p.getInputStream();
        DataInputStream dInStream = new DataInputStream(inStream);
        String str1 = String.valueOf(cmd);
        String str2 = str1 + "\n";
        dOutStream.writeBytes(str2);
        dOutStream.flush();
        String str3 = null;
        String line = "";
        while ((line = dInStream.readLine()) != null) {
            Log.d("result", str3);
            str3 += line;
        }
        dOutStream.writeBytes("exit\n");
        dOutStream.flush();
        p.waitFor();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return result;
    }
}

From source file:com.samczsun.helios.Helios.java

public static Process launchProcess(ProcessBuilder launch) throws IOException {
    Process process = launch.start();
    processes.add(process);//from w w w.j  av a 2 s.c om
    submitBackgroundTask(() -> {
        try {
            process.waitFor();
            if (!process.isAlive()) {
                processes.remove(process);
            }
        } catch (InterruptedException e) {
            ExceptionHandler.handle(e);
        }
    });
    return process;
}

From source file:com.samczsun.helios.Helios.java

public static void addToContextMenu() {
    try {// ww w  .  java  2  s . c o m
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
            Process process = Runtime.getRuntime().exec("reg add HKCR\\*\\shell\\helios\\command /f");
            process.waitFor();
            if (process.exitValue() == 0) {
                process = Runtime.getRuntime()
                        .exec("reg add HKCR\\*\\shell\\helios /ve /d \"Open with Helios\" /f");
                process.waitFor();
                if (process.exitValue() == 0) {
                    File currentJarLocation = getJarLocation();
                    if (currentJarLocation != null) {
                        File javaw = getJavawLocation();
                        if (javaw != null) {
                            process = Runtime.getRuntime()
                                    .exec("reg add HKCR\\*\\shell\\helios\\command /ve /d \"\\\""
                                            + javaw.getAbsolutePath() + "\\\" -jar \\\""
                                            + currentJarLocation.getAbsolutePath() + "\\\" \\\"%1\\\"\" /f");
                            process.waitFor();
                            if (process.exitValue() == 0) {
                                SWTUtil.showMessage("Done");
                            } else {
                                SWTUtil.showMessage("Failed to set context menu");
                            }
                        } else {
                            SWTUtil.showMessage("Could not set context menu - unable to find javaw.exe");
                        }
                    } else {
                        SWTUtil.showMessage("Could not set context menu - unable to find Helios.jar");
                    }
                } else {
                    SWTUtil.showMessage("Failed to set context menu");
                }
            } else {
                if (SWTUtil.promptForYesNo("UAC",
                        "Helios must be run as an administrator to do this. Relaunch as administrator?")) {
                    relaunchAsAdmin();
                }
            }
        } else {
            SWTUtil.showMessage("You may only do this on Windows");
        }
    } catch (Throwable t) {
        ExceptionHandler.handle(t);
    }
}

From source file:com.vmware.bdd.utils.CommonUtil.java

public static Process execCommand(String cmd) {
    if (cmd == null || cmd.isEmpty()) {
        return null;
    }// ww  w.  j a  v  a2s  .co m

    Process p = null;
    try {
        p = new ProcessBuilder(Arrays.asList(cmd.split(" "))).start();
        p.waitFor();
    } catch (Exception e) {
        p = null;
        logger.error("Failed to execute command " + cmd + " : " + e.getMessage());
    }

    return p;
}

From source file:automenta.climatenet.ImportKML.java

public static void exec(String cmd) {
    try {/*w w w  .j a  va  2 s .c om*/
        String[] cmdParm = { "/bin/sh", "-c", cmd };

        Process proc = Runtime.getRuntime().exec(cmdParm);
        IOUtils.copy(proc.getInputStream(), System.out);
        IOUtils.copy(proc.getErrorStream(), System.err);
        proc.waitFor();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}