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.flipkart.flux.examples.WorkflowExecutionDemo.java

/**
 * Helper method to execute a shell command
 * @param command shell command to run//from ww  w.j  a v  a2  s . c om
 * @return shell command's output
 */
private static String executeCommand(String command) throws IOException, InterruptedException {

    StringBuilder output = new StringBuilder();

    Process p;

    p = Runtime.getRuntime().exec(command);
    p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    //prints the output of the process
    String line = "";
    while ((line = reader.readLine()) != null) {
        output.append(line).append("\n");
    }

    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    //prints the error stream of the process
    String errorLine = "";
    while ((errorLine = stdError.readLine()) != null) {
        System.out.println(errorLine);
    }

    return output.toString();

}

From source file:br.on.daed.services.pdf.DadosMagneticos.java

public static byte[] gerarPDF(String ano, String tipo)
        throws IOException, InterruptedException, UnsupportedOperationException {

    File tmpFolder;/*from w w w . j ava 2  s  .  c o  m*/
    String folderName;

    Double anoDouble = Double.parseDouble(ano);
    List<String> dados = Arrays.asList(TIPO_DADOS_MAGNETICOS);

    byte[] ret = null;

    if (anoDouble >= ANO_MAGNETICO[0] && anoDouble < ANO_MAGNETICO[1] && dados.contains(tipo)) {

        do {

            folderName = "/tmp/dislin" + Double.toString(System.currentTimeMillis() * Math.random());
            tmpFolder = new File(folderName);

        } while (tmpFolder.exists());

        tmpFolder.mkdir();

        ProcessBuilder processBuilder = new ProcessBuilder("/opt/declinacao-magnetica/./gerar", ano, tipo);

        processBuilder.directory(tmpFolder);

        processBuilder.environment().put("LD_LIBRARY_PATH", "/usr/local/dislin");

        Process proc = processBuilder.start();

        proc.waitFor();

        ProcessHelper.outputProcess(proc);

        File arquivoServido = new File(folderName + "/dislin.pdf");

        FileInputStream fis = new FileInputStream(arquivoServido);

        ret = IOUtils.toByteArray(fis);

        processBuilder = new ProcessBuilder("rm", "-r", folderName);

        tmpFolder = new File("/");

        processBuilder.directory(tmpFolder);

        Process delete = processBuilder.start();

        delete.waitFor();

    } else {
        throw new UnsupportedOperationException("Entrada invlida");
    }
    return ret;
}

From source file:Main.java

static int waitForProcess(Process process) {
    if (process == null)
        return -1;
    try {//  w  w w . j  a  v a 2s .c o  m
        while (true) {
            try {
                return process.waitFor();
            } catch (InterruptedException ignored) {
            }
        }
    } finally {
        process.destroy();
    }
}

From source file:com.microsoft.alm.plugin.idea.common.setup.WindowsStartup.java

/**
 * Run script to launch elevated process to create registry keys
 *
 * @param regeditFilePath//from  w  w w. ja va2  s .  c o m
 */
private static void launchElevatedCreation(final String regeditFilePath) {
    try {
        final String[] cmd = { "cmd", "/C", "regedit", "/s", regeditFilePath };
        final ProcessBuilder processBuilder = new ProcessBuilder(cmd);
        final Process process = processBuilder.start();
        process.waitFor();
    } catch (IOException e) {
        logger.warn("Running regedit encountered an IOException: {}", e.getMessage());
    } catch (Exception e) {
        logger.warn("Waiting for the process to execute resulted in an error: " + e.getMessage());
    }
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 *///from ww w  .j  a  va  2s  .co m
public static void systemIn(String command, String data) throws Exception {
    String cmd[] = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:Main.java

public final static int waitForQuietly(Process p) {
    int exitCode = Integer.MAX_VALUE;
    if (p != null) {
        waitFor: do {
            try {
                exitCode = p.waitFor();
            } catch (InterruptedException e) {
                continue waitFor;
            }//w ww. j ava2s  .c o m
        } while (false);
    }
    return exitCode;
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 */// w  w w  .  j  a  va2s.  co  m
public static void systemIn(String command, String data) throws Exception {
    String[] cmd = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:com.ms.commons.utilities.CoreUtilities.java

/**
 * ???//  w  ww .jav a  2s .co m
 * 
 * @return
 */
public static String getHostName() {
    if (hostName != null) {
        return hostName;
    } else {
        try {
            String cmd = isWindowsOS() ? "hostname" : "/bin/hostname";
            Process process = Runtime.getRuntime().exec(cmd);
            process.waitFor();
            InputStream in = process.getInputStream();
            InputStreamReader inr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(inr);
            String wg = br.readLine();
            if (wg != null) {
                hostName = wg.trim();
            } else {
                hostName = "unknown hostname";
            }
        } catch (Exception e) {
            logger.error("Exception", e);
            hostName = "unknown hostname";
        }
    }
    return hostName;
}

From source file:com.ms.commons.utilities.CoreUtilities.java

/**
 * ?Linux?IP/*from ww  w  .  j  a v  a  2 s.  c o m*/
 * 
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private static String getIfconig() throws IOException, InterruptedException {
    String cmd = "/sbin/ifconfig";
    Process process = Runtime.getRuntime().exec(cmd);
    process.waitFor();
    InputStream in = process.getInputStream();
    InputStreamReader inr = new InputStreamReader(in);
    BufferedReader br = new BufferedReader(inr);
    String wg = "";
    while (true) {
        String line = br.readLine();
        if (line == null) {
            break;
        }
        if (line.indexOf("inet addr") != -1 || line.indexOf("inet ?") != -1) {
            wg = line.substring(line.indexOf(":") + 1, line.length());
            wg = wg.trim();
            int index = wg.indexOf("Bcast");
            if (index == -1) {
                index = wg.indexOf("");
            }
            if (index != -1) {
                wg = wg.substring(0, index);
                wg = wg.trim();
                break;
            } else {
                if (wg.length() > 14) {
                    wg = wg.substring(0, 14);
                }
            }
        }
    }
    return wg.trim();
}

From source file:es.bsc.demiurge.core.utils.CommandExecutor.java

/**
 * Executes a system command.//from   ww  w.  j  a  v  a2  s.  c  o m
 *
 * @param command the command
 * @return the result of executing the command
 */
public static String executeCommand(String command) throws IOException, InterruptedException {
    StringBuilder result = new StringBuilder();
    Process p;
    BufferedReader reader = null;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
            result.append(System.getProperty("line.separator"));
        }
        return result.toString();
    } finally {
        IOUtils.closeQuietly(reader);
    }
}