stanfordnlp/CoreNLP · error · IOException

cp: could not create target directory

Error message

cp: could not create target directory: <trueTarget>

What it means

When the cp target does not exist, the method tries to create it as a directory with trueTarget.mkdir(); if that fails it throws this IOException. Unlike the parented case, the created directory here IS the target itself.

Solutions

  1. Create the parent directories first with target.getParentFile().mkdirs() before calling cp
  2. Use mkdirs() semantics yourself: new File(path).mkdirs() then cp
  3. Verify the target's parent directory exists and is writable

Example fix

// before
IOUtils.cp(src, new File("out/a/b"), true); // out/a missing
// after
File target = new File("out/a/b");
target.mkdirs();
IOUtils.cp(src, target, true);
Defensive patterns

Strategy: validation

Validate before calling

File parent = target.getParentFile(); if (parent != null) parent.mkdirs();

Try / catch

try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().startsWith("cp: could not create target directory")) { target.getParentFile().mkdirs(); IOUtils.cp(src, target, true); } else throw e; }

Prevention

When it happens

Trigger: IOUtils.cp(dirA, nonExistentPathB, true) where nonExistentPathB.mkdir() returns false — typically because B's parent directory does not exist (mkdir does not create parents), or lacks write permission.

Common situations: Target path has missing intermediate directories (users expect mkdir -p behavior but this is plain mkdir); wrong absolute path; read-only parent directory.

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/499d39e28653d038. Report an issue: GitHub.

Appendix: source

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

        // 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);
    }
  }

  /**
   * @see IOUtils#cp(java.io.File, java.io.File, boolean)
   */
  public static void cp(File source, File target) throws IOException { cp(source, target, false); }

  /**

View on GitHub (pinned to 1b7edd19c4)