stanfordnlp/CoreNLP · error · IOException

cp: could not list files in source

Error message

cp: could not list files in source: <source>

What it means

During recursive directory copy, IOUtils.cp calls source.listFiles(); when that returns null — which File.listFiles() does if an I/O error occurs or the caller lacks read permission on the directory — cp throws this IOException instead of proceeding with a null array.

Solutions

  1. Check and fix read/execute permissions on the source directory for the running user (ls -ld source; chmod/chown).
  2. Verify the directory still exists immediately before copying (it may have been deleted concurrently).
  3. Confirm the directory is on an accessible (not failed/stale) filesystem, especially for network mounts.
  4. As an alternative, enumerate the tree yourself with Files.walk and copy per-file so you control error reporting.

Example fix

// before
IOUtils.cp(new File("/var/data/corpus"), new File("/backup/corpus"), true);
// after
File src = new File("/var/data/corpus");
if (!src.canRead()) {
  throw new IllegalStateException("Cannot read source directory: " + src.getAbsolutePath());
}
IOUtils.cp(src, new File("/backup/corpus"), true);
Defensive patterns

Strategy: validation

Validate before calling

File src = new File(sourceDir);
if (!src.isDirectory()) throw new IllegalArgumentException("Not a directory: " + src);
if (!src.canRead() || src.listFiles() == null)
  throw new IllegalStateException("Cannot list source directory: " + src.getAbsolutePath());

Try / catch

try {
  IOUtils.cp(src, target, true);
} catch (IOException e) {
  if (e.getMessage().startsWith("cp: could not list files")) {
    throw new IllegalStateException("Lost read access to " + src.getAbsolutePath() + " (perms? deleted? mount stale?)", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling IOUtils.cp(dirSource, target, true) where dirSource is a directory but listFiles() returns null: no read/execute permission on the source directory, the directory was removed concurrently, or a filesystem-level I/O error.

Common situations: Copying directory trees owned by another user (missing +x/+r bits); NFS/network mounts failing mid-walk; a directory deleted by a concurrent process (e.g. tmp cleaner) during the copy; sandboxed environments denying directory read access.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

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

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

View on GitHub (pinned to 1b7edd19c4)