junit-team/junit5 · error · JUnitException

Error redirecting stdout to file

Error message

Error redirecting stdout to file: ${stdoutPath}

What it means

StandardStreamsHandler.redirectStandardStreams() opens System.out's target file (Files.newOutputStream(stdoutPath)) when --stdout is supplied with a path different from --stderr. An IOException during open is wrapped as a JUnitException. This is the stdout-only branch of the redirect logic.

Solutions

  1. Create and verify the parent directory is writable before launching: Files.createDirectories(parent).
  2. Use an absolute file path (not a directory) with appropriate permissions.
  3. If the redirect is optional, omit --stdout to write to the terminal.

Example fix

# before
java -jar junit-platform-console-standalone.jar execute --stdout /var/locked/out.txt
# after
mkdir -p /tmp/out && chmod u+w /tmp/out && java -jar junit-platform-console-standalone.jar execute --stdout /tmp/out/out.txt
Defensive patterns

Strategy: validation

Validate before calling

// Before launching, verify the stdout target file is openable for writing.
import java.nio.file.*;
Path out = Paths.get(stdoutPath);
if (Files.isDirectory(out)) throw new IllegalArgumentException("--stdout target is a directory: " + out);
Path parent = out.getParent();
if (parent != null) Files.createDirectories(parent);
if (!Files.isWritable(parent)) throw new IllegalArgumentException("--stdout parent not writable: " + parent);

Prevention

When it happens

Trigger: Passing --stdout to a path that cannot be opened for writing while --stderr is null or different: missing parent dir, read-only location, path is a directory, or insufficient permissions.

Common situations: CI runners writing to a path whose directory was not created; pointing at a network share without write permission; accidentally reusing a directory path as the stdout file.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/efd30a46d890e387. Report an issue: GitHub.

Appendix: source

Thrown at junit-platform-console/src/main/java/org/junit/platform/console/command/StandardStreamsHandler.java:60

	 */
	public void redirectStandardStreams(@Nullable Path stdoutPath, @Nullable Path stderrPath) {
		if (isSameFile(stdoutPath, stderrPath)) {
			try {
				PrintStream commonStream = new PrintStream(Files.newOutputStream(stdoutPath), true);
				this.stdout = commonStream;
				this.stderr = commonStream;
			}
			catch (IOException ex) {
				throw new JUnitException("Error redirecting stdout and stderr to file: " + stdoutPath, ex);
			}
		}
		else {
			if (stdoutPath != null) {
				try {
					this.stdout = new PrintStream(Files.newOutputStream(stdoutPath), true);
				}
				catch (IOException ex) {
					throw new JUnitException("Error redirecting stdout to file: " + stdoutPath, ex);
				}
			}

			if (stderrPath != null) {
				try {
					this.stderr = new PrintStream(Files.newOutputStream(stderrPath), true);
				}
				catch (IOException ex) {
					throw new JUnitException("Error redirecting stderr to file: " + stderrPath, ex);
				}
			}
		}

		if (this.stdout != null) {
			System.setOut(this.stdout);
		}
		if (this.stderr != null) {
			System.setErr(this.stderr);

View on GitHub (pinned to f070c699a0)