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.googlecode.jsendnsca.MessagePayloadTest.java

private static String getShortHostNameFromOS() throws Exception {
    final Runtime runtime = Runtime.getRuntime();
    final Process process = runtime.exec("hostname");
    final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

    final String expectedHostName = input.readLine();
    input.close();//from   w ww .j  av  a 2s .co  m
    assertEquals(0, process.waitFor());

    return expectedHostName;
}

From source file:localization.split.java

public static boolean splitFile(String filepath, String passoloPath, boolean myesri) {
    File log = null;/*ww  w  .  j av  a2s.c o  m*/
    try {
        String path = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length());
        String folder = filepath.substring(0, filepath.lastIndexOf("\\"));
        String[] files = null;
        Vector<String> zFile;
        if (filepath.endsWith(".zip")) {
            zFile = readzipfile(filepath);
            for (String s : zFile) {
                if (s.endsWith(".lpu")) {
                    File unzipFolder = new File(s.substring(0, s.lastIndexOf("\\")));
                    splitFile(s, passoloPath, myesri);
                }
            }
        } else if (!filepath.endsWith(".lpu")) {
            return false;
        } else {
            if (!checkLPU(filepath, passoloPath)) {
                return false;
            }
            File ECI = new File(folder + "\\ECI");
            File AAC = new File(folder + "\\AAC");
            File TOIN = new File(folder + "\\TOIN");
            File LOIX = new File(folder + "\\LIOX");
            if (!ECI.exists()) {
                ECI.mkdir();
            }
            if (!AAC.exists()) {
                AAC.mkdir();
            }
            if (!TOIN.exists()) {
                TOIN.mkdir();
            }
            if (!LOIX.exists()) {
                LOIX.mkdir();
            }
            if (myesri == true) {
                files = new String[4];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[2] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[3] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                files = new String[6];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI_10.lpu";
                files[2] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[3] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[4] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX_10.lpu";
                files[5] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                File trgDir5 = new File(files[4]);
                File trgDir6 = new File(files[5]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                    FileUtils.copyFile(srcDir, trgDir5);
                    FileUtils.copyFile(srcDir, trgDir6);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            String logfile = folder + "\\" + path.substring(path.lastIndexOf("\\") + 1, path.lastIndexOf("."))
                    + ".log";
            log = new File(logfile);
            if (!log.exists()) {
                log.createNewFile();
            }

            for (int i = 0; i < files.length; i++) {
                int exitVal = 0;
                try {
                    String osName = System.getProperty("os.name");
                    String cmd = "cmd.exe /c " + passoloPath + " /openproject:" + files[i]
                            + " /runmacro=PslLpuSplitter_v3.bas" + " >> " + logfile;
                    System.out.println(cmd);
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(cmd);
                    exitVal = proc.waitFor();
                    System.out.println("Exit value: " + exitVal);
                    if (exitVal == 10) {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                File lpuFile = new File(files[i]);
                File logFile = new File(files[i].substring(0,
                        files[i].substring(0, files[i].lastIndexOf("\\")).lastIndexOf("\\") + 1)
                        + files[i].substring(files[i].lastIndexOf("\\") + 1, files[i].lastIndexOf("."))
                        + ".log");
                if (!lpuFile.exists()) {
                    logFile.delete();
                }
                File lpuFolder = new File(files[i].substring(0, files[i].lastIndexOf("\\")));
                if (lpuFolder.list().length == 0) {
                    lpuFolder.delete();
                }
            } // end for loop  
        }

    } catch (Exception e) {
        try {
            String content = e.getMessage();
            FileWriter fw = new FileWriter(log.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (Exception e1) {

        }

        return false;
    }
    return true;
}

From source file:abs.backend.erlang.ErlangTestDriver.java

public static boolean checkErlang() {
    if (SemanticTests.checkProg("erl")) {
        // http://stackoverflow.com/a/9561398/60462
        ProcessBuilder pb = new ProcessBuilder("erl", "-eval",
                "erlang:display(erlang:system_info(otp_release)), halt().", "-noshell");
        try {// www .  j  a v a  2  s.com
            Process p = pb.start();
            InputStreamReader r = new InputStreamReader(p.getInputStream());
            BufferedReader b = new BufferedReader(r);
            Assert.assertEquals(0, p.waitFor());
            String version = b.readLine();
            java.util.regex.Pattern pat = java.util.regex.Pattern.compile("\"(\\d*).*");
            Matcher m = pat.matcher(version);
            Assert.assertTrue("Could not identify Erlang version: " + version, m.matches());
            String v = m.group(1);
            Assume.assumeTrue("Need Erlang R17 or better.", Integer.parseInt(v) >= 17);
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
    }
    return true;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java

public static int executeCommandLine(String file, String dir) throws BusinessException {
    try {//  w ww .j a v a2s  . co  m
        Process process = Runtime.getRuntime().exec(file, null, new File(dir));
        process.waitFor();
        return process.exitValue();
    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(BusinessUtils.class.getName()).log(Level.SEVERE,
                "BusinessUtils.executeCommandLine Exception", ex);
        throw new BusinessException(ex);
    }
}

From source file:Main.java

private static boolean installOrUninstallApk(String apkPath, String installOruninstall, String rOrP) {
    Process process = null;
    DataOutputStream os = null;//from w ww .  ja  v a2  s.c  o  m
    String command = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        command = "pm " + installOruninstall + " " + rOrP + " " + apkPath + " \n";
        os.writeBytes(command);
        os.flush();
        os.close();
        process.waitFor();
        process.destroy();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:io.appium.java_client.service.local.AppiumServiceBuilder.java

private static void validateNodeJSVersion() {
    Runtime rt = Runtime.getRuntime();
    String result = null;//from   w w w .j  a va2s  .co m
    Process p = null;
    try {
        p = rt.exec(NODE_COMMAND_PREFIX + " node -v");
        p.waitFor();
        result = getProcessOutput(p.getInputStream());
    } catch (Exception e) {
        throw new InvalidNodeJSInstance("Node.js is not installed", e);
    } finally {
        if (p != null)
            p.destroy();
    }

    String versionNum = result.replace("v", "");
    String[] tokens = versionNum.split("\\.");
    if (Integer.parseInt(tokens[0]) < REQUIRED_MAJOR_NODE_JS
            || Integer.parseInt(tokens[1]) < REQUIRED_MINOR_NODE_JS)
        throw new InvalidNodeJSInstance("Current node.js version " + versionNum + "is lower than "
                + "required (" + REQUIRED_MAJOR_NODE_JS + "." + REQUIRED_MINOR_NODE_JS + " or greater)");
}

From source file:com.netscape.cmsutil.util.Utils.java

public static boolean exec(String cmd) {
    try {//from w ww .  j a v  a 2 s  . c  o m
        String cmds[] = null;
        if (isNT()) {
            // NT
            cmds = new String[3];
            cmds[0] = "cmd";
            cmds[1] = "/c";
            cmds[2] = cmd;
        } else {
            // UNIX
            cmds = new String[3];
            cmds[0] = "/bin/sh";
            cmds[1] = "-c";
            cmds[2] = cmd;
        }
        Process process = Runtime.getRuntime().exec(cmds);
        process.waitFor();

        if (process.exitValue() == 0) {
            /**
             * pOut = new BufferedReader(
             * new InputStreamReader(process.getInputStream()));
             * while ((l = pOut.readLine()) != null) {
             * System.out.println(l);
             * }
             **/
            return true;
        } else {
            /**
             * pOut = new BufferedReader(
             * new InputStreamReader(process.getErrorStream()));
             * l = null;
             * while ((l = pOut.readLine()) != null) {
             * System.out.println(l);
             * }
             **/
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return false;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method to execute command arrays./*from  w  w w.j a va 2s  .c  om*/
 * 
 * @param command
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static Result executeCommand(String... command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method executeCommand./*from   w w w  .j av  a2  s  .c o  m*/
 * 
 * @param command
 *            String
 * @return Result
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InterruptedException
 *             the interrupted exception
 */
public static Result executeCommand(String command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:io.appium.java_client.service.local.AppiumServiceBuilder.java

private static File findNodeInCurrentFileSystem() {
    Runtime rt = Runtime.getRuntime();
    String instancePath;//from w w w.jav a 2s.c  om
    Process p = null;
    try {
        p = rt.exec(returnCommandThatSearchesForDefaultNode());
        p.waitFor();
        instancePath = getProcessOutput(p.getInputStream());
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (p != null)
            p.destroy();
    }

    File result;
    if (StringUtils.isBlank(instancePath)
            || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER + APPIUM_NODE_MASK))
                    .exists())
        throw new InvalidServerInstanceException(
                "There is no installed nodes! Please install "
                        + " node via NPM (https://www.npmjs.com/package/appium#using-node-js) or download and "
                        + "install Appium app (http://appium.io/downloads.html)",
                new IOException("The installed appium node package has not been found."));

    return result;
}