junit-team/junit5 · error · JUnitException

Error redirecting stderr to file

Error message

Error redirecting stderr to file: ${stderrPath}

What it means

StandardStreamsHandler.redirectStandardStreams() opens System.err's target file (Files.newOutputStream(stderrPath)) when --stderr is supplied with a path different from --stdout. An IOException during open is wrapped as a JUnitException. This is the stderr-only branch, symmetric to the stdout branch.

Solutions

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

Example fix

# before
java -jar junit-platform-console-standalone.jar execute --stderr /nonexistent/err.txt
# after
mkdir -p /tmp/out && java -jar junit-platform-console-standalone.jar execute --stderr /tmp/out/err.txt
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

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

Common situations: Separating test stdout from stderr but pointing stderr at an unwritable location; CI writing logs into a directory that another stage cleaned up; relative paths resolving against an unexpected working directory.

Related errors


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

Appendix: source

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

				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);
		}
	}

	@Override
	public void close() {
		try {
			if (this.stdout != null) {
				this.stdout.close();
			}

View on GitHub (pinned to f070c699a0)