Java exec exec(String command)

Here you can find the source of exec(String command)

Description

exec

License

Apache License

Declaration

public static Process exec(String command) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*/*from w  w  w . j a  va  2s . c o  m*/
 * Copyright 2001-2013 Geert Bevin (gbevin[remove] at uwyn dot com)
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.io.IOException;

public class Main {
    public static Process exec(String command) throws IOException, InterruptedException {
        return exec(command, null);
    }

    public static Process exec(String command, String[] envp) throws IOException, InterruptedException {
        Process process;
        if (null == envp) {
            process = Runtime.getRuntime().exec(command);
        } else {
            process = Runtime.getRuntime().exec(command, envp);
        }

        process.waitFor();

        return process;
    }

    public static Process exec(String commands[], String[] envp) throws IOException, InterruptedException {
        Process process;
        if (null == envp) {
            process = Runtime.getRuntime().exec(commands);
        } else {
            process = Runtime.getRuntime().exec(commands, envp);
        }

        process.waitFor();

        return process;
    }

    public static Process exec(String command, long interval, long timeout)
            throws IOException, InterruptedException {
        return exec(command, null, interval, timeout);
    }

    public static Process exec(String[] commands, long interval, long timeout)
            throws IOException, InterruptedException {
        return exec(commands, null, interval, timeout);
    }

    public static synchronized Process exec(String command, String[] envp, long interval, long timeout)
            throws IOException, InterruptedException {
        Process process;
        if (null == envp) {
            process = Runtime.getRuntime().exec(command);
        } else {
            process = Runtime.getRuntime().exec(command, envp);
        }

        return processTimeout(process, interval, timeout);
    }

    public static synchronized Process exec(String[] commands, String[] envp, long interval, long timeout)
            throws IOException, InterruptedException {
        Process process;
        if (null == envp) {
            process = Runtime.getRuntime().exec(commands);
        } else {
            process = Runtime.getRuntime().exec(commands, envp);
        }

        return processTimeout(process, interval, timeout);
    }

    private static synchronized Process processTimeout(Process process, long interval, long timeout)
            throws InterruptedException {
        long time_waiting = 0;
        boolean process_finished = false;

        while (time_waiting < timeout && !process_finished) {
            process_finished = true;
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.fillInStackTrace();
                throw e;
            }

            try {
                process.exitValue();
            } catch (IllegalThreadStateException e) {
                // process hasn't finished yet
                process_finished = false;
            }
            time_waiting += interval;
        }

        if (process_finished) {
            return process;
        } else {
            process.destroy();
            return null;
        }
    }
}

Related

  1. exec(String cmd)
  2. exec(String cmd, File dir)
  3. exec(String command)
  4. exec(String command)
  5. exec(String command)
  6. exec(String command)
  7. exec(String command)
  8. exec(String command, String workingDir)
  9. exec(String command, String[] args_, String[] environment)