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:Main.java

public static String exec(String cmd) {
    StringBuilder sb = new StringBuilder();
    try {//from   www. ja  va  2  s  .  co  m
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(cmd);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        if (process.waitFor() != 0) {
            System.err.println("exit value = " + process.exitValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:net.pms.util.ProcessUtil.java

public static int waitFor(Process p) {
    int exit = -1;

    try {/*from w  ww  . j a va 2s.  c o m*/
        exit = p.waitFor();
    } catch (InterruptedException e) {
        Thread.interrupted();
    }

    return exit;
}

From source file:com.cprassoc.solr.auth.SolrAuthActionController.java

public static String doPushConfigToSolrAction(SecurityJson json) {
    String result = "";
    try {/*from   www.j  av  a2s  .  co  m*/
        String mime = "sh";
        if (Utils.isWindows()) {
            mime = "bat";
        }
        String pathToScript = System.getProperty("user.dir") + File.separator + "solrAuth." + mime;
        String jsonstr = json.export();
        String filePath = SolrAuthManager.getProperties().getProperty("solr.install.path") + File.separator
                + "security.json";
        File backup = new File("backup");
        if (!backup.exists()) {
            backup.mkdirs();
        }

        File file = new File(filePath);
        if (file.exists()) {
            // String newFilePath = SolrAuthManager.getProperties().getProperty("solr.install.path") + File.separator + System.currentTimeMillis() + "_security.json";
            // File newFile = new File(newFilePath);
            // file.renameTo(newFile);
            FileUtils.copyFileToDirectory(file, backup);
        }
        FileUtils.writeByteArrayToFile(file, jsonstr.getBytes());
        Thread.sleep(500);
        ProcessBuilder pb = new ProcessBuilder(pathToScript);

        Log.log("Run PUSH command");

        Process process = pb.start();
        if (process.waitFor() == 0) {
            result = "";
        } else {
            result = Utils.streamToString(process.getErrorStream());
            result += "\n" + Utils.streamToString(process.getInputStream());
            Log.log(result);
        }

    } catch (Exception e) {
        e.printStackTrace();
        result = e.getLocalizedMessage();
    }

    return result;
}

From source file:edu.umd.cs.marmoset.utilities.JProcess.java

public static void destroyProcessGroup(Process process, Logger log) {
    int pid = 0;/*from  w  w  w  .  ja  v a 2 s  .c om*/
    try {
        pid = getPid(process);

        log.debug("PID to be killed = " + pid);

        String command = "kill -9 -" + pid;

        String[] cmd = command.split("\\s+");

        Process kill = Runtime.getRuntime().exec(cmd);
        log.warn("Trying to kill the process group leader: " + command);
        kill.waitFor();
    } catch (IOException e) {
        // if we can't execute the kill command, then try to destroy the process
        log.warn("Unable to execute kill -9 -" + pid + "; now calling process.destroy()");
    } catch (InterruptedException e) {
        log.error("kill -9 -" + pid + " process was interrupted!  Now calling process.destroy()");
    } catch (IllegalAccessException e) {
        log.error("Illegal field access to PID field; calling process.destroy()", e);
    } catch (NoSuchFieldException e) {
        log.error("Cannot find PID field; calling process.destroy()", e);
    } finally {
        // call process.destroy() whether or not "kill -9 -<pid>" worked
        // in order to maintain proper internal state
        process.destroy();
    }
}

From source file:brut.util.OS.java

public static void exec(String[] cmd) throws BrutException {
    Process ps = null;
    try {//from  w  ww .  jav a 2 s  . c  om
        ps = Runtime.getRuntime().exec(cmd);

        new StreamForwarder(ps.getInputStream(), System.err).start();
        new StreamForwarder(ps.getErrorStream(), System.err).start();
        if (ps.waitFor() != 0) {
            throw new BrutException("could not exec command: " + Arrays.toString(cmd));
        }
    } catch (IOException ex) {
        throw new BrutException("could not exec command: " + Arrays.toString(cmd), ex);
    } catch (InterruptedException ex) {
        throw new BrutException("could not exec command: " + Arrays.toString(cmd), ex);
    }
}

From source file:Main.java

public static String runCommand(String command) {
    try {//from  w  w  w. ja  v  a  2 s  .c  o  m
        StringBuffer output = new StringBuffer();
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        reader.close();
        p.waitFor();
        return output.toString();
    } catch (InterruptedException | IOException e) {
        logError(e);
    }
    return "";
}

From source file:com.bukanir.android.utils.Utils.java

public static boolean isStorageVfat(Context context) {
    try {// ww w.  ja v  a  2  s . c o  m
        String cacheDir = context.getExternalCacheDir().toString();
        List<String> items = Arrays.asList(cacheDir.split("/"));
        String path = items.get(1) + "/" + items.get(2);

        String cmd = String.format("/system/bin/mount | grep '%s'", path);
        String[] command = { "/system/bin/sh", "-c", cmd };

        Process process = Runtime.getRuntime().exec(command, null, new File("/system/bin"));
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String line;
        String output = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = in.readLine()) != null) {
            output += line;
        }

        List<String> outputItems = Arrays.asList(output.split(" "));
        if (outputItems.size() >= 3) {
            if (outputItems.get(2).equals("vfat")) {
                return true;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.atlassian.labs.bamboo.git.edu.nyu.cs.javagit.client.cli.ProcessUtilities.java

/**
 * Waits for a process to terminate and then destroys it.
 *
 * @param p/*from w  w w.j  a  va2 s  .  c o  m*/
 *          The process to wait for and destroy.
 * @return The exit value of the process. By convention, 0 indicates normal termination.
 */
public static int waitForAndDestroyProcess(Process p, IParser parser) {
    /*
     * I'm not sure this is the best way to handle waiting for a process to complete. -- jhl388
     * 06.14.2008
     */
    while (true) {
        try {
            int i = p.waitFor();
            parser.processExitCode(p.exitValue());
            p.destroy();
            return i;
        } catch (InterruptedException e) {
            // TODO: deal with this interrupted exception in a better manner. -- jhl388 06.14.2008
            continue;
        }
    }
}

From source file:io.jmnarloch.cd.go.plugin.gradle.GradleTaskExecutor.java

/**
 * Executes the actual Gradle build.//ww w  .j  a v  a 2  s. c om
 *
 * @param builder the process builder
 * @param console the log output
 * @return the process return value
 * @throws IOException          if any error occurs during I/O operation
 * @throws InterruptedException if any error occurs during process execution
 */
private static int execute(ProcessBuilder builder, JobConsoleLogger console)
        throws IOException, InterruptedException {

    Process process = null;
    try {
        process = builder.start();

        console.readOutputOf(process.getInputStream());
        console.readErrorOf(process.getErrorStream());
        return process.waitFor();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

From source file:com.guye.baffle.util.OS.java

public static void exec(String[] cmd) throws BaffleException {
    Process ps = null;
    try {//w w w .ja va2 s. c  o m
        ps = Runtime.getRuntime().exec(cmd);

        new StreamForwarder(ps.getInputStream(), System.err).start();
        new StreamForwarder(ps.getErrorStream(), System.err).start();
        if (ps.waitFor() != 0) {
            throw new BaffleException("could not exec command: " + Arrays.toString(cmd));
        }
    } catch (IOException ex) {
        throw new BaffleException("could not exec command: " + Arrays.toString(cmd), ex);
    } catch (InterruptedException ex) {
        throw new BaffleException("could not exec command: " + Arrays.toString(cmd), ex);
    }
}