stanfordnlp/CoreNLP · error · IOException

Could not create directory <tgtDir.getAbsolutePath()>

Error message

Could not create directory <tgtDir.getAbsolutePath()>

What it means

IOUtils.ensureDir(File) throws this IOException when the path does not exist and File.mkdirs() fails, meaning the directory (including any needed parent directories) could not be created — typically due to missing write permission on the parent, a read-only filesystem, or a race where another process created a file at the path.

Solutions

  1. Check/fix permissions: ensure the process user can write to the parent directory (chmod/chown or run as the right user).
  2. Verify the filesystem is writable (not read-only, not full: df -h, mount flags).
  3. Log tgtDir.getAbsolutePath() and confirm each parent component is a directory, not a file.
  4. If this can occur transiently (race with another process), re-check exists()/isDirectory() after the failure before reporting an error.

Example fix

// before
IOUtils.ensureDir(new File("/var/lib/myapp/cache"));
// after
File dir = new File("/var/lib/myapp/cache");
if (!dir.getParentFile().canWrite()) {
  throw new IllegalStateException("No write permission on " + dir.getParentFile());
}
IOUtils.ensureDir(dir);
Defensive patterns

Strategy: validation

Validate before calling

File dir = new File(path);
File parent = dir.getParentFile();
if (parent != null && !parent.canWrite())
  throw new IllegalStateException("No write permission on " + parent.getAbsolutePath());
if (!dir.exists() && Files.isSymbolicLink(dir.toPath()))
  throw new IllegalStateException("Broken symlink at " + dir);

Try / catch

try {
  IOUtils.ensureDir(dir);
} catch (IOException e) {
  if (!dir.exists() && !dir.getParentFile().canWrite())
    throw new IllegalStateException("mkdirs failed: check permissions/disk on " + dir.getParent(), e);
  throw e;
}

Prevention

When it happens

Trigger: Calling IOUtils.ensureDir(tgtDir) where !tgtDir.exists() and tgtDir.mkdirs() returns false: no write permission on the parent directory, full/read-only disk, parent path component is a file, or a concurrent process created something at the path between the exists() check and mkdirs().

Common situations: Writing into system locations like /var or /usr without root; read-only container filesystems; running under a service account lacking permissions on the output root; NFS/permissions issues after deployment; output path colliding with an existing mount point.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/io/IOUtils.java:1746

  }

  /**
   * Given a filepath, makes sure a directory exists there.  If not, creates and returns it.
   * Same as ENSURE-DIRECTORY in CL.
   *
   * @param tgtDir The directory that you wish to ensure exists
   * @throws IOException If directory can't be created, is an existing file, or for other reasons
   */
  public static File ensureDir(File tgtDir) throws IOException {
    if (tgtDir.exists()) {
      if (tgtDir.isDirectory()) {
        return tgtDir;
      } else {
        throw new IOException("Could not create directory "+tgtDir.getAbsolutePath()+", as a file already exists at that path.");
      }
    } else {
      if ( ! tgtDir.mkdirs()) {
        throw new IOException("Could not create directory "+tgtDir.getAbsolutePath());
      }
      return tgtDir;
    }
  }

  /**
   * Given a filepath, delete all files in the directory recursively
   * @param dir Directory from which to delete files
   * @return {@code true} if the deletion is successful, {@code false} otherwise
   */
  public static boolean deleteDirRecursively(File dir) {
    if (dir.isDirectory()) {
      for (File f : dir.listFiles()) {
        boolean success = deleteDirRecursively(f);
        if (!success)
          return false;
      }
    }

View on GitHub (pinned to 1b7edd19c4)