stanfordnlp/CoreNLP · error · IOException

cp: cannot copy directory into regular file

Error message

cp: cannot copy directory into regular file: <target>

What it means

IOUtils.cp(File,File,boolean) implements a cp -r style recursive copy. When the target path already exists and is a regular file, a directory cannot be copied onto it, so the method throws this IOException to prevent destroying the file.

Solutions

  1. Delete or rename the existing regular-file target before calling cp, or point the target at a new path
  2. Verify the target path with File.isDirectory() before copying and fail early with a clear message
  3. Use a fresh target directory path per run (timestamped output dir)

Example fix

// before
IOUtils.cp(new File("data/src"), new File("out"), true); // out is an old file
// after
File out = new File("out");
if (out.exists() && !out.isDirectory()) { out.delete(); }
IOUtils.cp(new File("data/src"), out, true);
Defensive patterns

Strategy: validation

Validate before calling

if (target.exists() && !target.isDirectory()) { throw new IllegalStateException("cp target is a file: " + target); }

Try / catch

try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().startsWith("cp: cannot copy directory into regular file")) { /* fix target path or delete file */ } else throw e; }

Prevention

When it happens

Trigger: Calling IOUtils.cp(sourceDir, existingRegularFile, true) (or false) where target.exists() is true and target.isDirectory() is false.

Common situations: Script-driven copies that reuse a stale output filename; a previous run wrote the target as a file but the new source is a directory; typos in target paths in config.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

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

    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()) {
        // 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

View on GitHub (pinned to 1b7edd19c4)