Java exec exec(String pwd, boolean quiet, String... command)

Here you can find the source of exec(String pwd, boolean quiet, String... command)

Description

Execute command

License

Open Source License

Declaration

public static int exec(String pwd, boolean quiet, String... command) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    /** Execute command */
    public static int exec(String pwd, boolean quiet, String... command) {
        try {//from w  w w .  j av  a 2 s.c  o  m
            ProcessBuilder builder = new ProcessBuilder(command);
            builder = builder.directory(new File(pwd));
            if (!quiet)
                builder = builder.inheritIO();
            Process proc = builder.start();
            return proc.waitFor();
        } catch (IOException | InterruptedException e) {
            fatal("An error occured while running " + String.join(" ", command), e);
            return -1;
        }

    }

    /** Print fatal error and exit. */
    public static <T> T fatal(String message, Exception e) {
        System.out.println(message);
        if (e != null && System.getProperty("stacktrace") != null)
            e.printStackTrace();
        System.exit(0);
        return (T) null;
    }
}

Related

  1. exec(String command, String workingDir)
  2. exec(String command, String[] args_, String[] environment)
  3. exec(String command[], boolean stop)
  4. exec(String exec)
  5. exec(String program, String... args)
  6. exec(String... _command)
  7. exec(String... args)
  8. exec(String... cmd)
  9. exec(String... cmdarray)