stanfordnlp/CoreNLP · error · IOException

cp: cannot copy to directory

Error message

cp: cannot copy to directory: <recursive> (parent doesn't exist)

What it means

IOUtils.cp(File, File, boolean) throws this IOException when the parent directory of the target path does not exist, so the copy destination cannot be resolved. Note the message incorrectly interpolates the recursive flag where cp(1) would print the target path.

Solutions

  1. Create the target's parent directory first (target.getParentFile().mkdirs() or IOUtils.ensureDir(target.getParentFile())) before calling cp.
  2. Validate that target.getParentFile() exists and is a directory before copying.
  3. Fix the path construction/config so the destination points under an existing directory.

Example fix

// before
IOUtils.cp(srcFile, new File("/out/2026/sep/file.txt"), true);
// after
File target = new File("/out/2026/sep/file.txt");
IOUtils.ensureDir(target.getParentFile());
IOUtils.cp(srcFile, target, true);
Defensive patterns

Strategy: validation

Validate before calling

File target = new File(targetPath);
File parent = target.getParentFile();
if (parent == null || !parent.isDirectory())
  IOUtils.ensureDir(parent == null ? new File(".") : parent); // create missing parents first

Try / catch

try {
  IOUtils.cp(src, target, recursive);
} catch (IOException e) {
  if (e.getMessage().contains("(parent doesn't exist)")) {
    IOUtils.ensureDir(target.getParentFile());
    IOUtils.cp(src, target, recursive);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling IOUtils.cp(source, new File("a/b/c/d"), recursive) where directory a/b/c does not exist — target.getParentFile().exists() is false.

Common situations: Building target paths by concatenation where an intermediate directory was never created; output roots not yet initialized on a fresh environment; typos in directory names in config; deleted output directories between runs.

Related errors


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

Appendix: source

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

   * <p>An implementation of cp, as close to the Unix command as possible.
   * Both directories and files are valid for either the source or the target;
   * if the target exists, the semantics of Unix cp are [intended to be] obeyed.</p>
   *
   * @param source The source file or directory.
   * @param target The target to write this file or directory to.
   * @param recursive If true, recursively copy directory contents
   * @throws IOException If either the copy fails (standard IO Exception), or the command is invalid
   *                     (e.g., copying a directory without the recursive flag)
   */
  public static void cp(File source, File target, boolean recursive) throws IOException {
    // Error checks
    if (source.isDirectory() && !recursive) {
      // cp a b -- a is a directory
      throw new IOException("cp: omitting directory: " + source);
    }
    if (!target.getParentFile().exists()) {
      // cp a b/c/d/e -- b/c/d doesn't exist
      throw new IOException("cp: cannot copy to directory: " + recursive + " (parent doesn't exist)");
    }
    if (!target.getParentFile().isDirectory()) {
      // cp a b/c/d/e -- b/c/d is a regular file
      throw new IOException("cp: cannot copy to directory: " + recursive + " (parent isn't a directory)");
    }
    // Get true target
    File trueTarget;
    if (target.exists() && target.isDirectory()) {
      trueTarget = new File(target.getPath() + File.separator + source.getName());
    } else {
      trueTarget = target;
    }
    // Copy
    if (source.isFile()) {
      // Case: copying a file
      copyFile(source, trueTarget);
    } else if (source.isDirectory()) {
      // Case: copying a directory

View on GitHub (pinned to 1b7edd19c4)