Java exec exec(String cmd, File dir)

Here you can find the source of exec(String cmd, File dir)

Description

exec

License

fedora commons license

Declaration

public static Process exec(String cmd, File dir) 

Method Source Code


//package com.java2s;
/* The contents of this file are subject to the license and copyright terms
 * detailed in the license directory at the root of the source tree (also
 * available online at http://fedora-commons.org/license/).
 *//*from   w ww  . ja  v  a 2  s  .co m*/

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static Process exec(String cmd) {
        return exec(cmd, null, System.out, null);
    }

    public static Process exec(String[] cmd) {
        return exec(cmd, null, System.out, null);
    }

    public static Process exec(String cmd, OutputStream out) {
        return exec(cmd, null, out, null);
    }

    public static Process exec(String[] cmd, OutputStream out) {
        return exec(cmd, null, out, null);
    }

    public static Process exec(String cmd, File dir) {
        return exec(cmd, dir, System.out, null);
    }

    public static Process exec(String[] cmd, File dir) {
        return exec(cmd, dir, System.out, null);
    }

    public static Process exec(String cmd, File dir, OutputStream out, OutputStream err) {
        return exec(new String[] { cmd }, dir, out, err);
    }

    public static Process exec(String[] cmd, File dir, OutputStream out, OutputStream err) {
        Process cp = null;
        try {
            if (dir == null) {
                cp = Runtime.getRuntime().exec(cmd, getEnv());
            } else {
                cp = Runtime.getRuntime().exec(cmd, getEnv(), dir);
            }

            // Print stdio of cmd
            if (out != null) {
                PrintStream pout = new PrintStream(out);
                PrintStream perr = null;
                if (err != null) {
                    if (out == err) {
                        perr = pout;
                    } else {
                        perr = new PrintStream(err);
                    }
                } else {
                    perr = System.err;
                }
                String err_line;
                String in_line;
                BufferedReader input = new BufferedReader(new InputStreamReader(cp.getInputStream()));
                BufferedReader error = new BufferedReader(new InputStreamReader(cp.getErrorStream()));
                while (true) {
                    try {
                        cp.exitValue();
                        break; // process exited,
                    } catch (IllegalThreadStateException e) {
                        // process has not terminated check for output
                        if (error.ready()) {
                            err_line = error.readLine();
                            perr.println(err_line);
                        } else if (input.ready()) {
                            in_line = input.readLine();
                            pout.println(in_line);
                        } else {
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException ie) {
                                // don't worry, be happy
                            }
                        }
                    }
                }
                // Read any remaining buffered output from the process
                // after it terminates
                while (error.ready()) {
                    err_line = error.readLine();
                    perr.println(err_line);
                }
                while (input.ready()) {
                    in_line = input.readLine();
                    pout.println(in_line);
                }
                input.close();
                error.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cp;
    }

    private static String[] getEnv() {

        ArrayList<String> fixedEnv = new ArrayList<String>();

        Map<String, String> env = new HashMap<String, String>(System.getenv());

        if (!env.containsKey("FEDORA_HOME") && System.getProperty("fedora.home") != null) {
            env.put("FEDORA_HOME", System.getProperty("fedora.home"));
        }

        if (!env.containsKey("CATALINA_HOME") && System.getProperty("fedora.home") != null) {
            env.put("CATALINA_HOME", System.getProperty("fedora.home") + File.separator + "tomcat");
        }

        if (System.getProperty("online") != null) {
            env.put("ONLINE", System.getProperty("online"));
        }

        if (System.getProperty("offline") != null) {
            env.put("OFFLINE", System.getProperty("offline"));
        }

        for (String envName : env.keySet()) {
            fixedEnv.add(String.format("%s=%s", envName, env.get(envName)));
        }

        return fixedEnv.toArray(new String[0]);

    }
}

Related

  1. exec(String cmd)
  2. exec(String cmd)
  3. exec(String cmd)
  4. exec(String cmd)
  5. exec(String cmd)
  6. exec(String command)
  7. exec(String command)
  8. exec(String command)
  9. exec(String command)