/** * Creates a process builder with the specified arguments. * * <p> * Equivalent to calling <code>ProcessBuilder.create().command(args);</code> * * @param args The command-line arguments. * @return A new process builder. */ public static ProcBuilder create(Object...args) { return new ProcBuilder().command(args); }
/** * Append to the command arguments. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param args The command-line arguments. * @return This object (for method chaining). */ public ProcBuilder append(Object...args) { return appendIf(ANY, args); }
/** * Command arguments. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param args The command-line arguments. * @return This object (for method chaining). */ public ProcBuilder command(Object...args) { return commandIf(ANY, args); }
/** * Run this command and returns the output as a simple string. * * @return The output from the command. * @throws IOException * @throws InterruptedException */ public String getOutput() throws IOException, InterruptedException { StringWriter sw = new StringWriter(); pipeTo(sw).run(); return sw.toString(); }
/** * Pipe output to the specified writer, including the command and return code. * * <p> * The method can be called multiple times to write to multiple writers. * Don't close the writer afterwards. * * @param w The writer to pipe to. * @return This object (for method chaining). */ public ProcBuilder logTo(Writer w) { return logTo(w, false); }
/** * Append to the command arguments if the specified matcher matches. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param m The matcher. * @param args The command line arguments if matcher matches. * @return This object (for method chaining). */ public ProcBuilder appendIf(Matcher m, Object...args) { if (m.matches()) pb.command().addAll(toList(args)); return this; }
/** * Pipe output to the specified writer, but don't close the writer. * * @param w The writer to pipe to. * @return This object (for method chaining). */ public ProcBuilder pipeTo(Writer w) { return pipeTo(w, false); }
/** * Append the specified environment variables to the process. * * @param env The new set of environment variables. * @return This object (for method chaining). */ @SuppressWarnings({"rawtypes"}) public ProcBuilder env(Map env) { if (env != null) for (Map.Entry e : (Set<Map.Entry>)env.entrySet()) environment(e.getKey().toString(), e.getValue() == null ? null : e.getValue().toString()); return this; }
/** * Run this command and pipes the output to the specified writer or output stream. * * @return The exit code from the process. * @throws IOException * @throws InterruptedException */ public int run() throws IOException, InterruptedException { if (pb.command().size() == 0) throw new IOException("No command specified in ProcBuilder."); try { logWriters.append(divider).append('\n').flush(); logWriters.append(join(pb.command(), " ")).append('\n').flush(); p = pb.start(); IOPipe.create(p.getInputStream(), outWriters).lineProcessor(lp).byLines(byLines).run(); int rc = p.waitFor(); logWriters.append("Exit: ").append(String.valueOf(p.exitValue())).append('\n').flush(); if (rc > maxExitStatus) throw new IOException("Return code "+rc+" from command " + join(pb.command(), " ")); return rc; } finally { close(); } }
/** * Run this command and returns the output as a simple string. * * @return The output from the command. * @throws IOException * @throws InterruptedException */ public String getOutput() throws IOException, InterruptedException { StringWriter sw = new StringWriter(); pipeTo(sw).run(); return sw.toString(); }
/** * Pipe output to the specified writer, including the command and return code. * * <p> * The method can be called multiple times to write to multiple writers. * Don't close the writer afterwards. * * @param w The writer to pipe to. * @return This object (for method chaining). */ public ProcBuilder logTo(Writer w) { return logTo(w, false); }
/** * Append to the command arguments if the specified matcher matches. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param m The matcher. * @param args The command line arguments if matcher matches. * @return This object (for method chaining). */ public ProcBuilder appendIf(Matcher m, Object...args) { if (m.matches()) pb.command().addAll(toList(args)); return this; }
/** * Pipe output to the specified writer, but don't close the writer. * * @param w The writer to pipe to. * @return This object (for method chaining). */ public ProcBuilder pipeTo(Writer w) { return pipeTo(w, false); }
/** * Append the specified environment variables to the process. * * @param env The new set of environment variables. * @return This object (for method chaining). */ @SuppressWarnings({"rawtypes"}) public ProcBuilder env(Map env) { if (env != null) for (Map.Entry e : (Set<Map.Entry>)env.entrySet()) environment(e.getKey().toString(), e.getValue() == null ? null : e.getValue().toString()); return this; }
/** * Run this command and pipes the output to the specified writer or output stream. * * @return The exit code from the process. * @throws IOException * @throws InterruptedException */ public int run() throws IOException, InterruptedException { if (pb.command().size() == 0) throw new IOException("No command specified in ProcBuilder."); try { logWriters.append(divider).append('\n').flush(); logWriters.append(join(pb.command(), " ")).append('\n').flush(); p = pb.start(); IOPipe.create(p.getInputStream(), outWriters).lineProcessor(lp).byLines(byLines).run(); int rc = p.waitFor(); logWriters.append("Exit: ").append(String.valueOf(p.exitValue())).append('\n').flush(); if (rc > maxExitStatus) throw new IOException("Return code "+rc+" from command " + join(pb.command(), " ")); return rc; } finally { close(); } }
/** * Creates a process builder with the specified arguments. * * <p> * Equivalent to calling <code>ProcessBuilder.create().command(args);</code> * * @param args The command-line arguments. * @return A new process builder. */ public static ProcBuilder create(Object...args) { return new ProcBuilder().command(args); }
/** * Run this command and returns the output as a simple string. * * @return The output from the command. * @throws IOException * @throws InterruptedException */ public String getOutput() throws IOException, InterruptedException { StringWriter sw = new StringWriter(); pipeTo(sw).run(); return sw.toString(); }
/** * Pipe output to the specified writer, including the command and return code. * * <p> * The method can be called multiple times to write to multiple writers. * Don't close the writer afterwards. * * @param w The writer to pipe to. * @return This object (for method chaining). */ public ProcBuilder logTo(Writer w) { return logTo(w, false); }
/** * Append to the command arguments if the specified matcher matches. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param m The matcher. * @param args The command line arguments if matcher matches. * @return This object (for method chaining). */ public ProcBuilder appendIf(Matcher m, Object...args) { if (m.matches()) pb.command().addAll(toList(args)); return this; }
/** * Append to the command arguments. * * <p> * Arguments can be collections or arrays and will be automatically expanded. * * @param args The command-line arguments. * @return This object (for method chaining). */ public ProcBuilder append(Object...args) { return appendIf(ANY, args); }