@@ -154,25 +154,38 @@ public ProcessWrapper start() throws IOException {
154154 pb .directory (workDir );
155155 }
156156
157+ /*
158+ * Configure ProcessBuilder redirects that must be set before start().
159+ */
160+ if (input instanceof final File f ) {
161+ pb .redirectInput (f );
162+ }
163+ if (redirectErrorToOutput ) {
164+ pb .redirectErrorStream (true );
165+ } else if (redirectError instanceof final File f ) {
166+ pb .redirectError (f );
167+ }
168+ if (redirectOutput instanceof final File f ) {
169+ pb .redirectOutput (f );
170+ }
171+
157172 final Process proc = pb .start ();
158173
159- if (input != null ) {
174+ /*
175+ * Handle stdin/output redirection that uses the process streams.
176+ */
177+ if (input != null && !(input instanceof File )) {
160178 final var input = this .input ;
161- if (input instanceof final File f ) {
162- pb .redirectInput (f );
163- } else if (input instanceof final InputStream is ) {
179+ if (input instanceof final InputStream is ) {
164180 writeToStdIn (is , proc );
165181 } else if (input instanceof final CharSequence cs ) {
166182 writeToStdIn (cs , proc );
167183 }
168184 }
169- if (redirectErrorToOutput ) {
170- pb .redirectError ();
171- } else if (redirectError != null ) {
185+
186+ if (!redirectErrorToOutput && redirectError != null && !(redirectError instanceof File )) {
172187 final var redirectError = this .redirectError ;
173- if (redirectError instanceof final File f ) {
174- pb .redirectError (f );
175- } else if (redirectError instanceof final OutputStream os ) {
188+ if (redirectError instanceof final OutputStream os ) {
176189 redirect (proc .getErrorStream (), os );
177190 } else if (redirectError instanceof final Appendable appendable ) {
178191 redirect (proc .getErrorStream (), appendable );
@@ -181,11 +194,9 @@ public ProcessWrapper start() throws IOException {
181194 }
182195 }
183196
184- if (redirectOutput != null ) {
197+ if (redirectOutput != null && !( redirectOutput instanceof File ) ) {
185198 final var redirectOutput = this .redirectOutput ;
186- if (redirectOutput instanceof final File f ) {
187- pb .redirectOutput (f );
188- } else if (redirectOutput instanceof final OutputStream os ) {
199+ if (redirectOutput instanceof final OutputStream os ) {
189200 redirect (proc .getInputStream (), os );
190201 } else if (redirectOutput instanceof final Appendable appendable ) {
191202 redirect (proc .getInputStream (), appendable );
@@ -286,6 +297,7 @@ public Builder withRedirectError(final @Nullable PrintStream target) {
286297 public Builder withRedirectErrorToOutput () {
287298 if (redirectError != null )
288299 throw new IllegalArgumentException ("withRedirectErrorToOutput() and withRedirectError() are mutually exclusive." );
300+ redirectErrorToOutput = true ;
289301 return this ;
290302 }
291303
0 commit comments