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.github.stagirs.lingvo.syntax.disambiguity.mystem.MyStem.java

public static List<SyntaxItem[][]> process(List<String> text) throws IOException, InterruptedException {
    FileUtils.writeLines(new File("mystem_input"), "utf-8", text);
    Process process = Runtime.getRuntime()
            .exec(new String[] { "./mystem", "-c", "-i", "-d", "mystem_input", "mystem_output" });
    process.waitFor();
    InputStream is = process.getInputStream();
    try {/*from  w ww. ja va2 s.  c  o m*/
        List<String> lines = FileUtils.readLines(new File("mystem_output"), "utf-8");
        List<SyntaxItem[][]> resultList = new ArrayList<SyntaxItem[][]>();
        for (String line : lines) {
            String[] parts = line.split(" ");
            SyntaxItem[][] result = new SyntaxItem[parts.length][];
            for (int i = 0; i < result.length; i++) {
                if (parts[i].isEmpty()) {
                    result[i] = new SyntaxItem[0];
                    continue;
                }
                if (!parts[i].contains("{")) {
                    final String word = parts[i];
                    result[i] = new SyntaxItem[] { new SyntaxItem() {
                        @Override
                        public String getName() {
                            return word;
                        }

                        @Override
                        public String getType() {
                            return "";
                        }

                        @Override
                        public double getScore() {
                            return 0;
                        }
                    } };
                    continue;
                }
                final String word = parts[i].substring(0, parts[i].indexOf('{'));
                final String[] forms = parts[i].substring(parts[i].indexOf('{') + 1, parts[i].indexOf('}'))
                        .split("\\|");
                result[i] = new SyntaxItem[forms.length];
                for (int j = 0; j < result[i].length; j++) {
                    final String type = forms[j];
                    result[i][j] = new SyntaxItem() {
                        @Override
                        public String getName() {
                            return word;
                        }

                        @Override
                        public String getType() {
                            return type;
                        }

                        @Override
                        public double getScore() {
                            return 0;
                        }
                    };
                }
            }
            resultList.add(result);
        }
        return resultList;
    } finally {
        is.close();
    }
}

From source file:com.liferay.blade.samples.test.BladeCLIUtil.java

public static String execute(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*from   ww  w. java  2 s  .  c o m*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    List<String> errorList = new ArrayList<>();

    if (errorStream != null) {
        errorList.add(new String(IO.read(errorStream)));
    }

    List<String> filteredErrorList = new ArrayList<>();

    for (String string : errorList) {
        if (!string.isEmpty() && !string.contains("Picked up JAVA_TOOL_OPTIONS:")) {

            filteredErrorList.add(string);
        }
    }

    assertTrue(filteredErrorList.toString(), filteredErrorList.isEmpty());

    output = StringUtil.toLowerCase(output);

    return output;
}

From source file:Main.java

/**
 * Run command as root./*from  w  w  w.  j a va 2s  . c o m*/
 * 
 * @param command
 * @return true, if command was successfully executed
 */
private static boolean runAsRoot(final String command) {
    try {

        Process pro = Runtime.getRuntime().exec("su");
        DataOutputStream outStr = new DataOutputStream(pro.getOutputStream());

        outStr.writeBytes(command);
        outStr.writeBytes("\nexit\n");
        outStr.flush();

        int retval = pro.waitFor();

        return (retval == 0);

    } catch (Exception e) {

        return false;

    }
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

private static void decompressUtil(Path archiveFilePath, Path workingDir, boolean stripParent)
        throws Exception {
    try {/*from   w  w  w.  j a va 2  s . c  o m*/
        MutableList<String> tarArgs = Lists.mutable.of(UNIX_TAR, "xvzf",
                archiveFilePath.toFile().getAbsolutePath());

        if (stripParent) {
            tarArgs = tarArgs.with("--strip").with("1");
        }
        Process process = new ProcessBuilder(tarArgs.toList()).directory(workingDir.toFile()).start();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            logStdout(process.getInputStream());
            logStdErr(process.getErrorStream());
            throw new Exception("Failed to decompress");
        }
    } catch (Exception e) {
        throw new Exception("Failed to decompress", e);
    }
}

From source file:Main.java

public static String execCmd(String cmd) {

    String result = "";
    DataInputStream dis = null;//  w ww. java 2 s .co  m

    try {
        Process p = Runtime.getRuntime().exec(cmd);
        dis = new DataInputStream(p.getInputStream());
        String line = null;
        while ((line = dis.readLine()) != null) {
            line += "\n";
            result += line;
        }
        p.waitFor();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return result;
}

From source file:com.surfs.storage.common.util.CmdUtils.java

public static Map<String, Object> command(String cmd) {
    Map<String, Object> resp = new HashMap<String, Object>();
    BufferedReader bufRead = null;
    try {/*from w  w  w.j a va  2  s .c om*/
        //bufRead = executeCmdForReader(cmd);
        Process pro = Runtime.getRuntime().exec(cmd);
        bufRead = new BufferedReader(new InputStreamReader(pro.getInputStream(), "UTF-8"));
        // 0-success,others-failure
        int status = pro.waitFor();
        LogFactory.info("cmd:" + cmd);
        LogFactory.info("status:" + status);
        String response = null;
        while ((response = bufRead.readLine()) != null) {
            if (response.indexOf("avg") >= 0) {
                String[] ms = response.split("=")[1].split("\\/");
                resp.put("status", status);
                resp.put("latency", Float.parseFloat(ms[1]));
                LogFactory.info("latency:" + ms[1]);
                return resp;
            }
        }
    } catch (Exception e) {
        resp.put("status", 1);
        resp.put("response", "time out");
        return resp;
    } finally {
        if (bufRead != null)
            try {
                bufRead.close();
            } catch (Exception e) {
            }
    }
    resp.put("status", 1);
    resp.put("response", "time out");
    return resp;
}

From source file:javadepchecker.Main.java

private static Collection<String> getPackageJars(String pkg) {
    ArrayList<String> jars = new ArrayList<String>();
    try {//from www  . j  a  v a  2s. c o m
        Process p = Runtime.getRuntime().exec("java-config -p " + pkg);
        p.waitFor();
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String output = in.readLine();
        if (!output.trim().equals("")) {
            for (String jar : output.split(":")) {
                jars.add(jar);
            }
        }
    } catch (InterruptedException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    return jars;
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void compress(Path sourceDirectoryPath, Path archiveFilePath) throws Exception {
    try {//ww  w . j a  v a2s.co m
        ImmutableList<String> tarArgs = Lists.immutable.of(UNIX_TAR, "cvzf",
                archiveFilePath.toFile().getAbsolutePath(), ".");
        Process process = new ProcessBuilder(tarArgs.toList()).directory(sourceDirectoryPath.toFile()).start();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            logStdout(process.getInputStream());
            logStdErr(process.getErrorStream());
            throw new Exception("Failed to compress");
        }
    } catch (Exception e) {
        throw new Exception("Failed to compress", e);
    }
}

From source file:com.liferay.blade.tests.BladeCLI.java

public static String execute(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*from  w w  w .ja  v a  2  s  . c  o m*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    output = StringUtil.toLowerCase(output);

    return output;
}

From source file:com.liferay.blade.tests.BladeCLI.java

public static String startServerWindows(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("start");
    command.add("/b");
    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*  www.  ja v  a  2  s  . c  o  m*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    return output;
}