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:Main.java

public static void main(String[] args) throws Exception {
    Process process = Runtime.getRuntime().exec("ls -al");
    process.waitFor();// w ww.j  av  a2 s  .c  om

    int exitValue = process.exitValue();
    System.out.println("exitValue = " + exitValue);
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/* w  w  w . j a  v  a2  s .  c o  m*/
        // create a new process
        Process p = Runtime.getRuntime().exec("notepad.exe");

        p.destroy();

        System.out.println(p.exitValue());

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:ExecDemoFini.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;

    try {/* w  ww .j  ava  2  s.co  m*/
        p = r.exec("notepad");
        p.waitFor();
    } catch (Exception e) {
        System.out.println("Error executing notepad.");
    }
    System.out.println("Notepad returned " + p.exitValue());
}

From source file:com.frederikam.fredboat.bootloader.Bootloader.java

public static void main(String[] args) throws IOException, InterruptedException {
    OUTER: while (true) {
        InputStream is = new FileInputStream(new File("./bootloader.json"));
        Scanner scanner = new Scanner(is);
        JSONObject json = new JSONObject(scanner.useDelimiter("\\A").next());
        scanner.close();/* ww  w. ja  va 2s .  c  o m*/

        command = json.getJSONArray("command");
        jarName = json.getString("jarName");

        Process process = boot();
        process.waitFor();
        System.out.println("[BOOTLOADER] Bot exited with code " + process.exitValue());

        switch (process.exitValue()) {
        case ExitCodes.EXIT_CODE_UPDATE:
            System.out.println("[BOOTLOADER] Now updating...");
            update();
            break;
        case 130:
        case ExitCodes.EXIT_CODE_NORMAL:
            System.out.println("[BOOTLOADER] Now shutting down...");
            break OUTER;
        //SIGINT received or clean exit
        default:
            System.out.println("[BOOTLOADER] Now restarting..");
            break;
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;
    String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
    try {/*from www  . j  a v  a2s  .  c  o m*/
        p = r.exec(cmd);
        p.waitFor();
    } catch (Exception e) {
        System.out.println("error executing " + cmd[0]);
    }
    System.out.println(cmd[0] + " returned " + p.exitValue());
}

From source file:Console.java

public static void main(String[] args) {
    Runtime rt = Runtime.getRuntime();
    Process p = null;
    try {/*  ww  w.  j  a  v a2  s  . c  o m*/

        /*
         * Start a new process in which to execute the commands in cmd, using the environment in
         * env and use pwd as the current working directory.
         */
        p = rt.exec(args);// , env, pwd);
        new Console(p);
        System.exit(p.exitValue());
    } catch (IOException e) {
        /*
         * Couldn't even get the command to start. Most likely it couldn't be found because of a
         * typo.
         */
        System.out.println("Error starting: " + args[0]);
        System.out.println(e);
    }
}

From source file:org.mzd.shap.analysis.metagene.Metagene2008ToXML.java

public static void main(String[] args) throws IOException, BeanIOException {
    if (args.length != 2) {
        System.out.println("[input sequence] [output orfs]");
        System.exit(1);/*from   w ww .j av a 2s  .  com*/
    }

    Process p = Runtime.getRuntime().exec("metagene " + args[0]);
    try {
        synchronized (p) {
            StringWriter sw = new StringWriter();
            InputStreamReader isr = new InputStreamReader(p.getInputStream());
            int retVal;
            while (true) {
                p.wait(100);
                while (isr.ready()) {
                    sw.write(isr.read());
                }
                try {
                    retVal = p.exitValue();
                    break;
                } catch (IllegalThreadStateException ex) {
                    /*...*/}
            }

            // Just make sure stdout is completely empty.
            while (isr.ready()) {
                sw.write(isr.read());
            }

            if (retVal != 0) {
                System.out.println("Non-zero exist status [" + retVal + "]");
                InputStream is = null;
                try {
                    is = p.getErrorStream();
                    while (is.available() > 0) {
                        System.out.write(is.read());
                    }
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            } else {
                new Metagene2008ToXML().convert(sw.toString(), new File(args[1]));
            }

            System.exit(retVal);
        }
    } catch (InterruptedException ex) {
        /*...*/}
}

From source file:ExecDemoWait.java

public static void main(String argv[]) throws IOException {

    // A Runtime object has methods for dealing with the OS
    Runtime r = Runtime.getRuntime();
    Process p; // Process tracks one external native process
    BufferedReader is; // reader for output of process
    String line;/*from   www  .j  a  v  a  2 s.co  m*/

    // Our argv[0] contains the program to run; remaining elements
    // of argv contain args for the target program. This is just
    // what is needed for the String[] form of exec.
    p = r.exec(argv);

    System.out.println("In Main after exec");

    // getInputStream gives an Input stream connected to
    // the process p's standard output. Just use it to make
    // a BufferedReader to readLine() what the program writes out.
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while ((line = is.readLine()) != null)
        System.out.println(line);

    System.out.println("In Main after EOF");
    System.out.flush();
    try {
        p.waitFor(); // wait for process to complete
    } catch (InterruptedException e) {
        System.err.println(e); // "Can'tHappen"
        return;
    }
    System.err.println("Process done, exit status was " + p.exitValue());
    return;
}

From source file:Main.java

/**
 * Check if a process is still alive.//  ww w .j a v  a  2  s  .  co  m
 */
public static boolean isProcessAlive(Process process) {
    try {
        process.exitValue();
        return false;
    } catch (IllegalThreadStateException e) {
        return true;
    }
}

From source file:Main.java

/** Test whether the given process has terminated. */
public static boolean processIsTerminated(Process p) {
    try {/*  ww  w  .  jav a2 s. c  om*/
        p.exitValue();
        return true;
    } catch (IllegalThreadStateException e) {
        return false;
    }
}