{"record":{"id":"ceb043e7a8d3bcf0","repo":"junit-team/junit5","slug":"error-redirecting-stdout-and-stderr-to-file-std","errorCode":null,"errorMessage":"Error redirecting stdout and stderr to file: ${stdoutPath}","messagePattern":"Error redirecting stdout and stderr 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":51,"sourceCode":"\t *\n\t * <p>If the paths are the same, both streams are redirected to the same file.\n\t *\n\t * <p>The default charset is used for writing to the files.\n\t *\n\t * @param stdoutPath the file path for standard output, or {@code null} to\n\t * indicate no redirection\n\t * @param stderrPath the file path for standard error, or {@code null} to\n\t * indicate no redirection\n\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);","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-console/src/main/java/org/junit/platform/console/command/StandardStreamsHandler.java#L33-L69","documentation":"StandardStreamsHandler.redirectStandardStreams() opens a single combined output stream for System.out and System.err when the same path is supplied for both (--stdout and --stderr point to one file). If Files.newOutputStream(path) throws IOException (e.g. permission denied, parent missing, path is a directory), a JUnitException wraps it.","triggerScenarios":"Passing --stdout and --stderr to the same path where that file cannot be created/opened: a directory path, a read-only location, a missing parent directory, or a permissions issue.","commonSituations":"Redirecting console launcher output to a file path whose parent directory does not exist, is not writable, or is itself a directory; running in a sandboxed/CI environment with a restricted writable area.","solutions":["Ensure the parent directory of the target file exists and is writable: create it and check permissions before running.","Do not point --stdout/--stderr at a directory; supply a file path.","If you do not need separate streams, this branch is fine — just fix the path; if you need them separate, use distinct paths (which then take the other branches).","Use an absolute path to avoid ambiguity about the process working directory."],"exampleFix":"# before\njava -jar junit-platform-console-standalone.jar execute --stdout /no/such/dir/out.txt --stderr /no/such/dir/out.txt\n# after\nmkdir -p /tmp/out && java -jar junit-platform-console-standalone.jar execute --stdout /tmp/out/log.txt --stderr /tmp/out/log.txt","handlingStrategy":"validation","validationCode":"// Before launching, ensure the shared stdout/stderr target file is writable.\nimport java.nio.file.*;\nPath out = Paths.get(stdoutPath); // same path for --stdout and --stderr\nif (Files.isDirectory(out)) throw new IllegalArgumentException(\"target is a directory: \" + out);\nPath parent = out.getParent();\nif (parent != null) { Files.createDirectories(parent); }\nif (Files.exists(parent) && !Files.isWritable(parent))\n  throw new IllegalArgumentException(\"parent not writable: \" + parent);\n","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create and verify the parent directory before redirecting streams.","Use absolute file paths (not directories) for --stdout/--stderr.","In CI, write redirects under a known writable temp/workspace dir."],"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"}