stanfordnlp/CoreNLP · error · IOException

cp: cannot copy to directory

Error message

cp: cannot copy to directory: <recursive> (parent isn't a directory)

What it means

IOUtils.cp(File, File, boolean) throws this IOException when the target's parent path exists but is a regular file, not a directory — e.g. copying to a/b/c/d where a/b/c is a file. As with the sibling error, the message interpolates the recursive flag rather than the offending path.

Solutions

  1. Inspect target.getParentFile(): remove/rename the file occupying the intended directory path.
  2. Validate before copying: parent must exist AND be a directory; fail early with the real path in the message.
  3. Correct the path construction so no file is used as a path component.

Example fix

// before
IOUtils.cp(src, new File("/out/data.txt/item.bin"), true);
// after
File target = new File("/out/data.txt/item.bin");
File parent = target.getParentFile();
if (parent.exists() && !parent.isDirectory()) {
  throw new IllegalStateException("Not a directory: " + parent.getAbsolutePath());
}
IOUtils.cp(src, target, true);
Defensive patterns

Strategy: validation

Validate before calling

File target = new File(targetPath);
File parent = target.getParentFile();
if (parent != null && parent.exists() && !parent.isDirectory())
  throw new IllegalStateException("Parent is a file, not a directory: " + parent.getAbsolutePath());

Try / catch

try {
  IOUtils.cp(src, target, recursive);
} catch (IOException e) {
  if (e.getMessage().contains("(parent isn't a directory)")) {
    throw new IllegalStateException("Path component is a file: " + target.getParentFile().getAbsolutePath(), e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling IOUtils.cp(source, target, recursive) where target.getParentFile() exists but target.getParentFile().isDirectory() is false.

Common situations: A path component expected to be a directory is actually a file (leftover file created by a previous bug or bind-mount); output path built from a filename prefix that collides with an existing file; Windows/Unix path handling mistakes producing "data.txt/sub" style paths.

Related errors


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

Appendix: source

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

   * @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
      File[] children = source.listFiles();
      if (children == null) { throw new IOException("cp: could not list files in source: " + source); }

      if (target.exists()) {

View on GitHub (pinned to 1b7edd19c4)