{"record":{"id":"efd30a46d890e387","repo":"junit-team/junit5","slug":"error-redirecting-stdout-to-file-stdoutpath","errorCode":null,"errorMessage":"Error redirecting stdout to file: ${stdoutPath}","messagePattern":"Error redirecting stdout to file: (.+?)","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-platform-console/src/main/java/org/junit/platform/console/command/StandardStreamsHandler.java","lineNumber":60,"sourceCode":"\t */\n\tpublic void redirectStandardStreams(@Nullable Path stdoutPath, @Nullable Path stderrPath) {\n\t\tif (isSameFile(stdoutPath, stderrPath)) {\n\t\t\ttry {\n\t\t\t\tPrintStream commonStream = new PrintStream(Files.newOutputStream(stdoutPath), true);\n\t\t\t\tthis.stdout = commonStream;\n\t\t\t\tthis.stderr = commonStream;\n\t\t\t}\n\t\t\tcatch (IOException ex) {\n\t\t\t\tthrow new JUnitException(\"Error redirecting stdout and stderr to file: \" + stdoutPath, ex);\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (stdoutPath != null) {\n\t\t\t\ttry {\n\t\t\t\t\tthis.stdout = new PrintStream(Files.newOutputStream(stdoutPath), true);\n\t\t\t\t}\n\t\t\t\tcatch (IOException ex) {\n\t\t\t\t\tthrow new JUnitException(\"Error redirecting stdout to file: \" + stdoutPath, ex);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (stderrPath != null) {\n\t\t\t\ttry {\n\t\t\t\t\tthis.stderr = new PrintStream(Files.newOutputStream(stderrPath), true);\n\t\t\t\t}\n\t\t\t\tcatch (IOException ex) {\n\t\t\t\t\tthrow new JUnitException(\"Error redirecting stderr to file: \" + stderrPath, ex);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (this.stdout != null) {\n\t\t\tSystem.setOut(this.stdout);\n\t\t}\n\t\tif (this.stderr != null) {\n\t\t\tSystem.setErr(this.stderr);","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-console/src/main/java/org/junit/platform/console/command/StandardStreamsHandler.java#L42-L78","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Create and verify the parent directory is writable before launching: Files.createDirectories(parent).","Use an absolute file path (not a directory) with appropriate permissions.","If the redirect is optional, omit --stdout to write to the terminal."],"exampleFix":"# before\njava -jar junit-platform-console-standalone.jar execute --stdout /var/locked/out.txt\n# after\nmkdir -p /tmp/out && chmod u+w /tmp/out && java -jar junit-platform-console-standalone.jar execute --stdout /tmp/out/out.txt","handlingStrategy":"validation","validationCode":"// Before launching, verify the stdout target file is openable for writing.\nimport java.nio.file.*;\nPath out = Paths.get(stdoutPath);\nif (Files.isDirectory(out)) throw new IllegalArgumentException(\"--stdout target is a directory: \" + out);\nPath parent = out.getParent();\nif (parent != null) Files.createDirectories(parent);\nif (!Files.isWritable(parent)) throw new IllegalArgumentException(\"--stdout parent not writable: \" + parent);\n","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pre-create the output directory in your CI script.","Use absolute paths for --stdout.","Confirm the process user has write permission on the target directory."],"tags":["console-launcher","io","filesystem","stream-redirection"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}