{"record":{"id":"a88f25e61ee9b467","repo":"stanfordnlp/CoreNLP","slug":"could-not-create-output-stream-cannot-write-file","errorCode":null,"errorMessage":"Could not create output stream (cannot write file): \" + value","messagePattern":"Could not create output stream \\(cannot write file\\): \" \\+ value","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":664,"sourceCode":"        throw new RuntimeException(e);\n      }\n    } else if (PrintWriter.class.isAssignableFrom(clazz)) {\n      // (case: input stream)\n      if (value.equalsIgnoreCase(\"stdout\") || value.equalsIgnoreCase(\"out\")) { return (E) new PrintWriter(System.out); }\n      if (value.equalsIgnoreCase(\"stderr\") || value.equalsIgnoreCase(\"err\")) { return (E) new PrintWriter(System.err); }\n      try {\n        return (E) IOUtils.getPrintWriter(value);\n      } catch (IOException e) {\n        throw new RuntimeException(e);\n      }\n    } else if (OutputStream.class.isAssignableFrom(clazz)) {\n      // (case: output stream)\n      if (value.equalsIgnoreCase(\"stdout\") || value.equalsIgnoreCase(\"out\")) { return (E) System.out; }\n      if (value.equalsIgnoreCase(\"stderr\") || value.equalsIgnoreCase(\"err\")) { return (E) System.err; }\n      File toWriteTo = cast(value, File.class);\n      try {\n        if (toWriteTo == null || (!toWriteTo.exists() && !toWriteTo.createNewFile())) {\n          throw new IllegalStateException(\"Could not create output stream (cannot write file): \" + value);\n        }\n        return (E) IOUtils.getFileOutputStream(value);\n      } catch (IOException e) {\n        throw new RuntimeException(e);\n      }\n    } else if (InputStream.class.isAssignableFrom(clazz)) {\n      // (case: input stream)\n      if (value.equalsIgnoreCase(\"stdin\") || value.equalsIgnoreCase(\"in\")) { return (E) System.in; }\n      try {\n        return (E) IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(value);\n      } catch (IOException e) {\n        throw new RuntimeException(e);\n      }\n    } else {\n      try {\n        // (case: can parse from string)\n        Method decode = clazz.getMethod(\"fromString\", String.class);\n        return (E) decode.invoke(MetaClass.create(clazz), value);","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L646-L682","documentation":"When casting a string to an OutputStream, MetaClass accepts 'stdout'/'stderr' aliases or treats the string as a file path. If the target File is null, does not exist, and cannot be created (createNewFile fails), it throws IllegalStateException 'Could not create output stream (cannot write file)'. This guards output redirection configured via string properties.","triggerScenarios":"Setting an output-stream property to a path in a nonexistent directory, a read-only location, or a path whose parent folders are missing so File.createNewFile() returns false; passing a null/empty value that casts to a null File.","commonSituations":"CoreNLP -output/-outputDirectory config pointing at a missing or unwritable directory; running in a container with a read-only filesystem; relative paths resolved against an unexpected working directory.","solutions":["Verify the parent directory exists and is writable; create it before running (e.g. new File(dir).mkdirs())","Use 'stdout' or 'stderr' if you only need console output","Run with sufficient filesystem permissions or choose a writable path (e.g. /tmp)","Check the working directory when using relative paths"],"exampleFix":"// before\nprops.setProperty(\"output\", \"/nonexistent/dir/out.txt\");\n// after\nnew File(\"/out/dir\").mkdirs();\nprops.setProperty(\"output\", \"/out/dir/out.txt\");","handlingStrategy":"validation","validationCode":"File f = new File(path);\nFile parent = f.getAbsoluteFile().getParentFile();\nif (parent == null || (!parent.isDirectory() && !parent.mkdirs()))\n  throw new IllegalArgumentException(\"Cannot create parent dir: \" + parent);\nif (!f.exists() && !f.createNewFile()) throw new IllegalArgumentException(\"Cannot create file: \" + f);\nif (!f.canWrite()) throw new IllegalArgumentException(\"Not writable: \" + f);","typeGuard":null,"tryCatchPattern":"try {\n  OutputStream os = MetaClass.cast(path, OutputStream.class);\n} catch (IllegalStateException | RuntimeException e) {\n  logger.warning(\"Output path unusable: \" + path + \" — falling back to stdout\");\n  OutputStream os2 = System.out;\n}","preventionTips":["Create parent directories before configuring output paths","Prefer absolute paths over relative ones to avoid working-directory surprises","Check container/filesystem write permissions when deploying","Use 'stdout'/'stderr' aliases when console output suffices"],"tags":["io","file-write","configuration","corenlp"],"backgroundTag":"file-write-failed","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}