Example usage for java.lang Process exitValue

List of usage examples for java.lang Process exitValue

Introduction

In this page you can find the example usage for java.lang Process exitValue.

Prototype

public abstract int exitValue();

Source Link

Document

Returns the exit value for the process.

Usage

From source file:msec.org.Tools.java

static public int runCommand(String[] cmd, StringBuffer sb, boolean waitflag) {

    Process pid = null;
    ProcessBuilder build = new ProcessBuilder(cmd);
    build.redirectErrorStream(true);/*  w  w  w.  j  a  v a 2s. c om*/
    try {
        pid = build.start();
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    if (sb != null) {
        //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
        InputStream in = pid.getInputStream();
        byte[] buf = new byte[10240];
        try {
            while (true) {
                int len = in.read(buf);
                if (len <= 0) {
                    break;
                }
                sb.append(new String(buf, 0, len));
            }
        } catch (Exception e) {
        }

    }
    if (waitflag) {
        try {
            pid.waitFor();
            int v = pid.exitValue();
            pid.destroy();
            return v;
        } catch (Exception e) {
        }
    }
    return 0;
}

From source file:com.github.sakserv.minicluster.yarn.util.ExecShellCliParser.java

public int runCommand() throws Exception {
    String command = getCommand();
    String stdoutFile = getStdoutPath();
    String stderrFile = getStderrPath();

    Process p = Runtime.getRuntime().exec(command.split(" "));

    String stdout = getOutput(p.getInputStream());
    String stderr = getOutput(p.getErrorStream());

    writeOutputToFile(stdout, new File(stdoutFile));
    writeOutputToFile(stderr, new File(stderrFile));

    p.waitFor();/*from w w  w  . jav  a 2  s  . co  m*/
    return p.exitValue();
}

From source file:com.google.dart.server.internal.remote.StdioServerSocket.java

/**
 * Wait up to 5 seconds for process to gracefully exit, then forcibly terminate the process if it
 * is still running.//  www.  ja va  2 s  . c  om
 */
@Override
public void stop() {
    if (process == null) {
        return;
    }
    final Process processToStop = process;
    process = null;
    long endTime = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < endTime) {
        try {
            int exit = processToStop.exitValue();
            if (exit != 0) {
                Logging.getLogger()
                        .logInformation("Non-zero exit code: " + exit + " for\n   " + analysisServerPath);
            }
            return;
        } catch (IllegalThreadStateException e) {
            //$FALL-THROUGH$
        }
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            //$FALL-THROUGH$
        }
    }
    processToStop.destroy();
    Logging.getLogger().logInformation("Terminated " + analysisServerPath);
}

From source file:com.migratebird.script.runner.impl.Application.java

public ProcessOutput execute(boolean logCommand, String... arguments) {
    try {/*from   w w w.ja  va 2  s.com*/
        List<String> commandWithArguments = getProcessArguments(arguments);

        ProcessBuilder processBuilder = createProcessBuilder(commandWithArguments);
        Process process = processBuilder.start();
        OutputProcessor outputProcessor = new OutputProcessor(process);
        outputProcessor.start();
        process.waitFor();

        String output = outputProcessor.getOutput();
        int exitValue = process.exitValue();

        logOutput(commandWithArguments, output, logCommand);
        return new ProcessOutput(output, exitValue);

    } catch (Exception e) {
        throw new MigrateBirdException("Failed to execute command.", e);
    }
}

From source file:MSUmpire.Utility.MSConvert.java

public void Convert() {
    try {//from  ww  w .  j  a v a  2s  .co m
        String[] msconvertcmd = { msconvertpath, "--mzXML", "--32", "-z", SpectrumPath, "-o",
                FilenameUtils.getFullPath(SpectrumPath) };
        Process p = Runtime.getRuntime().exec(msconvertcmd);
        Logger.getRootLogger().info("MGF file coversion by msconvert.exe...." + SpectrumPath);
        Logger.getRootLogger().debug("Command: " + Arrays.toString(msconvertcmd));
        PrintThread printThread = new PrintThread(p);
        printThread.start();
        p.waitFor();
        if (p.exitValue() != 0) {
            Logger.getRootLogger().info("msconvert : " + SpectrumPath + " failed");
            //PrintOutput(p);
            return;
        }
    } catch (IOException | InterruptedException ex) {
        java.util.logging.Logger.getLogger(MSConvert.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.sap.prd.mobile.ios.mios.PListAccessor.java

public void addElement(String key, String type) throws IOException {
    if (!plist.exists()) {
        throw new FileNotFoundException("Plist file '" + plist + "' not found.");
    }/*from   ww w  . j  ava2  s  . co  m*/
    try {
        String command = "/usr/libexec/PlistBuddy -x -c \"Add :" + key + " " + type + "  " + "\" \""
                + plist.getAbsolutePath() + "\"";
        System.out.println("[INFO] PlistBuddy Add command is: '" + command + "'.");
        String[] args = new String[] { "bash", "-c", command };
        Process p = Runtime.getRuntime().exec(args);
        p.waitFor();
        int exitValue = p.exitValue();
        if (exitValue != 0) {
            String errorMessage = "n/a";
            try {
                errorMessage = new Scanner(p.getErrorStream(), Charset.defaultCharset().name())
                        .useDelimiter("\\Z").next();
            } catch (Exception ex) {
                System.out.println("[ERROR] Exception caught during retrieving error message of command '"
                        + command + "': " + ex);
            }
            throw new IllegalStateException("Execution of \"" + StringUtils.join(args, " ")
                    + "\" command failed: " + errorMessage + ". Exit code was: " + exitValue);
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.dbmaintain.script.runner.impl.Application.java

public ProcessOutput execute(boolean logCommand, String... arguments) {
    try {//from  ww  w  .  j  a  v  a 2s. co  m
        List<String> commandWithArguments = getProcessArguments(arguments);

        ProcessBuilder processBuilder = createProcessBuilder(commandWithArguments);
        Process process = processBuilder.start();
        OutputProcessor outputProcessor = new OutputProcessor(process);
        outputProcessor.start();
        process.waitFor();

        String output = outputProcessor.getOutput();
        int exitValue = process.exitValue();

        logOutput(commandWithArguments, output, logCommand);
        return new ProcessOutput(output, exitValue);

    } catch (Exception e) {
        throw new DbMaintainException("Failed to execute command.", e);
    }
}

From source file:com.sap.prd.mobile.ios.mios.PListAccessor.java

public void updateStringValue(String key, String value) throws IOException {
    if (!plist.exists()) {
        throw new FileNotFoundException("Plist file '" + plist + "' not found.");
    }/*from   w  w  w.  j  a  va2 s  .c o m*/

    try {
        String command = "/usr/libexec/PlistBuddy -x -c \"Set :" + key + " " + value + "\" \""
                + plist.getAbsolutePath() + "\"";
        System.out.println("[INFO] PlistBuddy Set command is: '" + command + "'.");
        String[] args = new String[] { "bash", "-c", command };
        Process p = Runtime.getRuntime().exec(args);
        p.waitFor();
        int exitValue = p.exitValue();
        if (exitValue != 0) {
            String errorMessage = "n/a";
            try {
                errorMessage = new Scanner(p.getErrorStream(), Charset.defaultCharset().name())
                        .useDelimiter("\\Z").next();
            } catch (Exception ex) {
                System.out.println("[ERROR] Exception caught during retrieving error message of command '"
                        + command + "': " + ex);
            }
            throw new IllegalStateException("Execution of \"" + StringUtils.join(args, " ")
                    + "\" command failed: " + errorMessage + ". Exit code was: " + exitValue);
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sap.prd.mobile.ios.mios.PListAccessor.java

public void addStringValue(String key, String value) throws IOException {
    if (!plist.exists()) {
        throw new FileNotFoundException("Plist file '" + plist + "' not found.");
    }//from   w  w  w  .j ava2s .  c  om

    try {
        String command = "/usr/libexec/PlistBuddy -x -c \"Add :" + key + " string " + value + "\" \""
                + plist.getAbsolutePath() + "\"";
        System.out.println("[INFO] PlistBuddy Add command is: '" + command + "'.");
        String[] args = new String[] { "bash", "-c", command };
        Process p = Runtime.getRuntime().exec(args);
        p.waitFor();
        int exitValue = p.exitValue();
        if (exitValue != 0) {
            String errorMessage = "n/a";
            try {
                errorMessage = new Scanner(p.getErrorStream(), Charset.defaultCharset().name())
                        .useDelimiter("\\Z").next();
            } catch (Exception ex) {
                System.out.println("[ERROR] Exception caught during retrieving error message of command '"
                        + command + "': " + ex);
            }
            throw new IllegalStateException("Execution of \"" + StringUtils.join(args, " ")
                    + "\" command failed: " + errorMessage + ". Exit code was: " + exitValue);
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sap.prd.mobile.ios.mios.PListAccessor.java

public void addStringValueToDict(String key, String value, String dictKey) throws IOException {
    if (!plist.exists()) {
        throw new FileNotFoundException("Plist file '" + plist + "' not found.");
    }/*from w  w w .ja  v a 2s  .  c om*/

    try {
        String command = "/usr/libexec/PlistBuddy -x -c \"Add :" + dictKey + ":" + key + " string " + value
                + "\" \"" + plist.getAbsolutePath() + "\"";
        System.out.println("[INFO] PlistBuddy Add command is: '" + command + "'.");
        String[] args = new String[] { "bash", "-c", command };
        Process p = Runtime.getRuntime().exec(args);
        p.waitFor();
        int exitValue = p.exitValue();
        if (exitValue != 0) {
            String errorMessage = "n/a";
            try {
                errorMessage = new Scanner(p.getErrorStream(), Charset.defaultCharset().name())
                        .useDelimiter("\\Z").next();
            } catch (Exception ex) {
                System.out.println("[ERROR] Exception caught during retrieving error message of command '"
                        + command + "': " + ex);
            }
            throw new IllegalStateException("Execution of \"" + StringUtils.join(args, " ")
                    + "\" command failed: " + errorMessage + ". Exit code was: " + exitValue);
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}