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:net.dv8tion.discord.Yui.java

private static void relaunchInUTF8() throws InterruptedException, UnsupportedEncodingException {
    System.out.println("BotLauncher: We are not running in UTF-8 mode! This is a problem!");
    System.out.println("BotLauncher: Relaunching in UTF-8 mode using -Dfile.encoding=UTF-8");

    String[] command = new String[] { "java", "-Dfile.encoding=UTF-8", "-jar",
            Yui.getThisJarFile().getAbsolutePath() };

    //Relaunches the bot using UTF-8 mode.
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.inheritIO(); //Tells the new process to use the same command line as this one.
    try {/*from ww  w .j av a 2s .  c o m*/
        Process process = processBuilder.start();
        process.waitFor(); //We wait here until the actual bot stops. We do this so that we can keep using the same command line.
        System.exit(process.exitValue());
    } catch (IOException e) {
        if (e.getMessage().contains("\"java\"")) {
            System.out.println(
                    "BotLauncher: There was an error relaunching the bot. We couldn't find Java to launch with.");
            System.out.println("BotLauncher: Attempted to relaunch using the command:\n   "
                    + StringUtils.join(command, " ", 0, command.length));
            System.out.println(
                    "BotLauncher: Make sure that you have Java properly set in your Operating System's PATH variable.");
            System.out.println("BotLauncher: Stopping here.");
        } else {
            e.printStackTrace();
        }
    }
}

From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java

public static String exec(String... cmdLine) {
    try {//from   w w  w . j a  v  a 2s .c o  m
        Process process = new ProcessBuilder(cmdLine).start();
        process.waitFor();
        return new String(IOUtils.toByteArray(process.getInputStream()));
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.moz.fiji.schema.util.JvmId.java

/**
 * Returns the Unix process ID of this JVM.
 *
 * @return the Unix process ID of this JVM.
 *//*  w ww.  j  av a 2s  .c o  m*/
public static int getPid() {
    try {
        final Process process = new ProcessBuilder("/bin/sh", "-c", "echo $PPID").start();
        try {
            Preconditions.checkState(process.waitFor() == 0);
        } catch (InterruptedException ie) {
            throw new RuntimeInterruptedException(ie);
        }
        final String pidStr = IOUtils.toString(process.getInputStream()).trim();
        return Integer.parseInt(pidStr);
    } catch (IOException ioe) {
        throw new FijiIOException(ioe);
    }
}

From source file:eu.cloud4soa.c4sgitservice.utils.Util.java

public static synchronized int replaceBlockWithSedForAuthorizedKeys(String filename, String pubkeyid) {
    int retvalue = 1; //1=ERROR 0=OK

    try {// ww w. java  2  s.  c  om
        String sedblock = createSedBlockForAuthorizedKeys(pubkeyid);
        System.out.println(" sed -e " + sedblock + " " + filename);
        String[] command = new String[] { "sed", "-i", // -i is for replace // -e is used for piping
                sedblock, filename };
        System.out.println(command.toString());
        Process child = Runtime.getRuntime().exec(command);
        child.waitFor();
        //Get the input stream and read from it
        InputStream in = child.getInputStream();
        int c;
        while ((c = in.read()) != -1) {
            System.out.print((char) c);
        }
        in.close();
        retvalue = child.exitValue();
        System.out.println("Replace file returned: " + retvalue);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retvalue;
}

From source file:eu.cloud4soa.c4sgitservice.utils.Util.java

public static synchronized int replaceBlockWithSedForProxyGit(String filename, String tag) {
    int retvalue = 1; //1=ERROR 0=OK

    try {//from   w  w w  .  j  a  v a  2  s .  c  o m
        String sedblock = createSedBlockForProxyGit(tag);
        System.out.println(" sed -e " + sedblock + " " + filename);
        String[] command = new String[] { "sed", "-i", // -i is for replace // -e is used for piping
                sedblock, filename };
        System.out.println(command.toString());
        Process child = Runtime.getRuntime().exec(command);
        child.waitFor();
        //Get the input stream and read from it
        InputStream in = child.getInputStream();
        int c;
        while ((c = in.read()) != -1) {
            System.out.print((char) c);
        }
        in.close();
        retvalue = child.exitValue();
        System.out.println("Replace file returned: " + retvalue);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retvalue;
}

From source file:Main.java

/**
 *  Method to find the number of files from a particular folder on the device.
 *//*from  ww  w.  j a va2  s  . c  o  m*/
public static int findNumberOfFiles(String dFolderPath) throws InterruptedException {
    int numOfFiles = 0;
    Thread.sleep(500);
    try {
        ProcessBuilder process = new ProcessBuilder("adb", "shell", "ls", dFolderPath, "|", "wc", "-l");
        Process p = process.start();
        //Thread.sleep(5000);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        p.waitFor();
        while ((line = br.readLine()) != null) {
            if (!line.equals(""))
                numOfFiles = Integer.parseInt(line);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    return numOfFiles;
}

From source file:hudson.os.WindowsUtil.java

/**
 * Creates an NTFS junction point if supported. Similar to symbolic links, NTFS provides junction points which
 * provide different features than symbolic links.
 * @param junction NTFS junction point to create
 * @param target target directory to junction
 * @return the newly created junction point
 * @throws IOException if the call to mklink exits with a non-zero status code
 * @throws InterruptedException if the call to mklink is interrupted before completing
 * @throws AssertionError if this method is called on a non-Windows platform
 *//* ww  w .  jav  a  2  s  .co m*/
public static @Nonnull File createJunction(@Nonnull File junction, @Nonnull File target)
        throws IOException, InterruptedException {
    assertTrue(Functions.isWindows());
    Process mklink = execCmd("mklink", "/J", junction.getAbsolutePath(), target.getAbsolutePath());
    int result = mklink.waitFor();
    if (result != 0) {
        String stderr = IOUtils.toString(mklink.getErrorStream());
        String stdout = IOUtils.toString(mklink.getInputStream());
        throw new IOException("Process exited with " + result + "\nStandard Output:\n" + stdout
                + "\nError Output:\n" + stderr);
    }
    return junction;
}

From source file:Main.java

private static void confirmExitValueIs(int expected, Process process) {
    // Consider if we need to add timeout logic here.
    while (true) {
        try {/*from w  w w .j a  va 2  s .  co  m*/
            process.waitFor();
            break;
        } catch (InterruptedException exception) {
            // do nothing, try to wait again
        }
    }

    int actual = process.exitValue();

    if (expected != actual) {
        throw new RuntimeException("Exit value of process was " + actual + " but expected " + expected);
    }
}

From source file:io.github.retz.executor.FileManager.java

private static void decompress(File file, String dir) throws IOException {
    LOG.info("{} needs decompression: starting", file);
    String[] cmd = { "tar", "xf", file.getAbsolutePath(), "-C", dir };
    ProcessBuilder pb = new ProcessBuilder().command(cmd).inheritIO();
    try {/* w w  w  .java 2s .  co m*/
        Process p = pb.start();
        int r = p.waitFor();
        if (r == 0) {
            LOG.info("file {} successfully decompressed", file);
        } else {
            LOG.error("Failed decompression of file {}: {}", file, r);
        }
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
    }
}

From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java

public static void restoreMongoDbFromDump(String dumpLocation) throws IOException, InterruptedException {
    logger.info("restoring DB from " + dumpLocation);

    Process exec = Runtime.getRuntime().exec("mongorestore " + dumpLocation);
    exec.waitFor();
    String line;/*from   w ww  . ja  va2s  . c  om*/
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore output:" + line);
    }
    bufferedReader.close();
    bufferedReader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore errorOutput:" + line);
    }
    bufferedReader.close();

    logger.info("mongorestore exit value: " + exec.exitValue());
}