stanfordnlp/CoreNLP · error · IOException

cp: could not create directory

Error message

cp: could not create directory: <trueTarget>

What it means

During a recursive cp onto an existing directory, trueTarget (target/sourceName) does not exist and File.mkdir() fails, so the method throws this IOException. mkdir() fails only when the directory cannot be created (permissions, or a same-named file appearing concurrently).

Solutions

  1. Check write permission on the existing target directory and fix it (chmod/chown) or run as an account with write access
  2. Ensure no concurrent process creates a file with the source's name inside the target
  3. Wrap the copy in try-catch for IOException and surface the path so the user can fix permissions

Example fix

// before
IOUtils.cp(srcDir, new File("/var/log/app"), true); // read-only dir
// after
File dest = new File("/var/log/app");
if (!dest.canWrite()) { throw new IllegalStateException("No write permission on " + dest); }
IOUtils.cp(srcDir, dest, true);
Defensive patterns

Strategy: validation

Validate before calling

File tt = new File(target, source.getName()); if (!tt.exists() && !target.canWrite()) { throw new IllegalStateException("Cannot write into " + target); }

Try / catch

try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().startsWith("cp: could not create directory")) { /* fix permissions / stop concurrent writer */ } else throw e; }

Prevention

When it happens

Trigger: IOUtils.cp(dirA, existingDirB, true) where existingDirB exists but existingDirB/dirA.getName() cannot be created via mkdir() (no write permission on B, or a file with that name created between the exists() check and mkdir()).

Common situations: Read-only or root-owned output directories; running under a restricted service account; race with another process writing into the target.

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/47a985682059ab9f. Report an issue: GitHub.

Appendix: source

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

      copyFile(source, trueTarget);
    } else if (source.isDirectory()) {
      // Case: copying a directory
      File[] children = source.listFiles();
      if (children == null) { throw new IOException("cp: could not list files in source: " + source); }

      if (target.exists()) {
        // Case: cp -r a b -- b exists
        if (!target.isDirectory()) {
          // cp -r a b -- b is a regular file
          throw new IOException("cp: cannot copy directory into regular file: " + target);
        }
        if (trueTarget.exists() && !trueTarget.isDirectory()) {
          // cp -r a b -- b/a is not a directory
          throw new IOException("cp: overwriting a file with a directory: " + trueTarget);
        }
        if (!trueTarget.exists() && !trueTarget.mkdir()) {
          // cp -r a b -- b/a cannot be created
          throw new IOException("cp: could not create directory: " + trueTarget);
        }
      } else {
        // Case: cp -r a b -- b does not exist
        assert trueTarget == target;
        if (!trueTarget.mkdir()) {
          // cp -r a b -- cannot create b as a directory
          throw new IOException("cp: could not create target directory: " + trueTarget);
        }
      }
      // Actually do the copy
      for (File child : children) {
        File childTarget = new File(trueTarget.getPath() + File.separator + child.getName());
        cp(child, childTarget, recursive);
      }
    } else {
      throw new IOException("cp: unknown file type: " + source);
    }
  }

View on GitHub (pinned to 1b7edd19c4)