Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 1&1 Internet AG,
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <>.
 package net.sf.beezle.sushi.launcher;
 import java.util.List;

Configures and executes an operating system process. This class wraps a ProcessBuilder to simplify usage. In particular, most methods return this so you can configure and execute a program in a single expression (short methods names further simplify this). In addition, you can easily get process output as a string. None-zero exit codes of a process are reported as ExitCode exceptions. This helps to improve reliability because it's harder to ignore exceptions than to ignore return codes. Note that the first "arg" passed to an instance of this class is actually not an argument, but the name of the program or script to be executed. I accept this inconsistency because it simplifies the api and allows for shorter method names.
 public class Launcher {
     private final ProcessBuilder builder;
     private String encoding;
     public Launcher(String... args) {
         this. = new ProcessBuilder();
     public Launcher(FileNode dirString... args) {
     //-- configuration
     public Launcher env(String keyString value) {
         return this;
     public Launcher arg(String... args) {
         for (String a : args) {
         return this;
     public Launcher args(List<Stringargs) {
         return this;

initializes the directory to execute the command in
     public Launcher dir(FileNode dir) {
         return dir(dir.getFile(), dir.getWorld().getSettings().);

You'll normally use the dir(FileNode) method instead.
     public Launcher dir(File dirString encoding) {;
         this. = encoding;
         return this;
     //-- execution
     public void execNoOutput() throws Failure {
         String result;
         result = exec();
         if (result.trim().length() > 0) {
             throw new Failure(this.command().get(0) + ": unexpected output " + result);
     public String exec() throws Failure {
        ByteArrayOutputStream result;
        result = new ByteArrayOutputStream();
        return string(result.toByteArray());
    public void exec(OutputStream allthrows Failure {
    public void exec(OutputStream stdoutOutputStream stderrthrows Failure {

Executes a command in this directory, wired with the specified streams. None of the argument stream is closed.

stderr may be null (which will redirect the error stream to stdout.
stdin may be null
    public void exec(OutputStream stdoutOutputStream stderrInputStream stdinboolean stdinSpecialthrows Failure {
        Process process;
        int exit;
        String output;
        PumpStream psout;
        PumpStream pserr;
        PumpStream psin;
        InputPumpStream psinSpecial;
        if (.directory() == null) {
            // builder.start() does not check, I would not detect the problem until process.waitFor is called
            // - that's to late because buffer would also be null
            throw new IllegalStateException("Missing directory. Call dir() before invoking this method");
        .redirectErrorStream(stderr == null);
        try {
            process = .start();
        } catch (IOException e) {
            throw new Failure(thise);
        psout = new PumpStream(process.getInputStream(), stdoutfalse);
        if (stderr != null) {
            pserr = new PumpStream(process.getErrorStream(), stderrfalse);
        } else {
            pserr = null;
        if (stdin != null) {
            if (stdinSpecial) {
                psinSpecial = new InputPumpStream(stdinprocess.getOutputStream());
                psin = null;
            } else {
                psin = new PumpStream(stdinprocess.getOutputStream(), true);
                psinSpecial = null;
        } else {
            psin = null;
            psinSpecial = null;
        if (pserr != null) {
        try {
            exit = process.waitFor();
        } catch (InterruptedException e) {
            throw new Interrupted(e);
        if (psin != null) {
        if (psinSpecial != null) {
        if (exit != 0) {
            if (stderr == null && stdout instanceof ByteArrayOutputStream) {
                output = string(((ByteArrayOutputStreamstdout).toByteArray());
            } else {
                output = "";
            throw new ExitCode(thisexitoutput);

If you need access to the internals. Most applications won't need this method.
    public ProcessBuilder getBuilder() {
        return ;
    public String toString() {
        return "[" + .directory() + "] " + ..join(.command());
    private String string(byte[] bytes) {
        try {
            return new String(bytes);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
New to GrepCode? Check out our FAQ X