stanfordnlp/CoreNLP · error · IllegalStateException

Could not create output stream (cannot write file): " +…

Error message

Could not create output stream (cannot write file): " + value

What it means

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.

Solutions

  1. Verify the parent directory exists and is writable; create it before running (e.g. new File(dir).mkdirs())
  2. Use 'stdout' or 'stderr' if you only need console output
  3. Run with sufficient filesystem permissions or choose a writable path (e.g. /tmp)
  4. Check the working directory when using relative paths

Example fix

// before
props.setProperty("output", "/nonexistent/dir/out.txt");
// after
new File("/out/dir").mkdirs();
props.setProperty("output", "/out/dir/out.txt");
Defensive patterns

Strategy: validation

Validate before calling

File f = new File(path);
File parent = f.getAbsoluteFile().getParentFile();
if (parent == null || (!parent.isDirectory() && !parent.mkdirs()))
  throw new IllegalArgumentException("Cannot create parent dir: " + parent);
if (!f.exists() && !f.createNewFile()) throw new IllegalArgumentException("Cannot create file: " + f);
if (!f.canWrite()) throw new IllegalArgumentException("Not writable: " + f);

Try / catch

try {
  OutputStream os = MetaClass.cast(path, OutputStream.class);
} catch (IllegalStateException | RuntimeException e) {
  logger.warning("Output path unusable: " + path + " — falling back to stdout");
  OutputStream os2 = System.out;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/a88f25e61ee9b467. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/MetaClass.java:664

        throw new RuntimeException(e);
      }
    } else if (PrintWriter.class.isAssignableFrom(clazz)) {
      // (case: input stream)
      if (value.equalsIgnoreCase("stdout") || value.equalsIgnoreCase("out")) { return (E) new PrintWriter(System.out); }
      if (value.equalsIgnoreCase("stderr") || value.equalsIgnoreCase("err")) { return (E) new PrintWriter(System.err); }
      try {
        return (E) IOUtils.getPrintWriter(value);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else if (OutputStream.class.isAssignableFrom(clazz)) {
      // (case: output stream)
      if (value.equalsIgnoreCase("stdout") || value.equalsIgnoreCase("out")) { return (E) System.out; }
      if (value.equalsIgnoreCase("stderr") || value.equalsIgnoreCase("err")) { return (E) System.err; }
      File toWriteTo = cast(value, File.class);
      try {
        if (toWriteTo == null || (!toWriteTo.exists() && !toWriteTo.createNewFile())) {
          throw new IllegalStateException("Could not create output stream (cannot write file): " + value);
        }
        return (E) IOUtils.getFileOutputStream(value);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else if (InputStream.class.isAssignableFrom(clazz)) {
      // (case: input stream)
      if (value.equalsIgnoreCase("stdin") || value.equalsIgnoreCase("in")) { return (E) System.in; }
      try {
        return (E) IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(value);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    } else {
      try {
        // (case: can parse from string)
        Method decode = clazz.getMethod("fromString", String.class);
        return (E) decode.invoke(MetaClass.create(clazz), value);

View on GitHub (pinned to 1b7edd19c4)