apache/hadoop · error · FileNotFoundException

File " + original + " does not exist

Error message

File " + original + " does not exist

What it means

Thrown in Stat.parseExecResult when the stat output line ends with 'No such file or directory' or 'Not a directory', i.e. the shell command failed with ENOENT or ENOTDIR. Stat translates those well-known stderr strings into FileNotFoundException with the original path, giving callers the standard 'file does not exist' semantics.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Stat.java:121

          path.toString() };
    } else {
      throw new UnsupportedOperationException(
          "stat is not supported on this platform");
    }
  }

  @Override
  protected void parseExecResult(BufferedReader lines) throws IOException {
    // Reset stat
    stat = null;

    String line = lines.readLine();
    if (line == null) {
      throw new IOException("Unable to stat path: " + original);
    }
    if (line.endsWith("No such file or directory") ||
        line.endsWith("Not a directory")) {
      throw new FileNotFoundException("File " + original + " does not exist");
    }
    if (line.endsWith("Too many levels of symbolic links")) {
      throw new IOException("Possible cyclic loop while following symbolic" +
          " link " + original);
    }
    // 6,symbolic link,6,1373584236,1373584236,lrwxrwxrwx,andrew,andrew,`link' -> `target'
    // OR
    // 6,symbolic link,6,1373584236,1373584236,lrwxrwxrwx,andrew,andrew,'link' -> 'target'
    StringTokenizer tokens = new StringTokenizer(line, ",");
    try {
      long length = Long.parseLong(tokens.nextToken());
      boolean isDir = tokens.nextToken().equalsIgnoreCase("directory") ? true
          : false;
      // Convert from seconds to milliseconds
      long modTime = Long.parseLong(tokens.nextToken())*1000;
      long accessTime = Long.parseLong(tokens.nextToken())*1000;
      String octalPerms = tokens.nextToken();
      // FreeBSD has extra digits beyond 4, truncate them

View on GitHub (pinned to 2add963021)

Solutions

  1. Verify the path and each parent component: the file must exist and no parent may be a plain file
  2. Handle FileNotFoundException as a non-fatal 'already gone' case in cleanup code
  3. For pure existence checks prefer fs.exists(p) or Files.exists instead of full stat parsing

Example fix

// before
FileStatus st = new Stat(path, false, shell).getFileStatus();

// after
java.nio.file.Path p = java.nio.file.Paths.get(path.toString());
if (!java.nio.file.Files.exists(p)) {
  throw new java.io.FileNotFoundException(path.toString());
}
Defensive patterns

Strategy: validation

Validate before calling

java.nio.file.Path nio = java.nio.file.Paths.get(path.toString());
if (!java.nio.file.Files.exists(nio)) {
  throw new java.io.FileNotFoundException(path.toString());
}
FileStatus st = new Stat(path, false, shell).getFileStatus();

Try / catch

try {
  st = stat.getFileStatus();
} catch (java.io.FileNotFoundException e) {
  // already gone: benign for cleanup loops
  return;
}

Prevention

When it happens

Trigger: new Stat(path, ...).getFileStatus() where path does not exist, or where a parent component of the path is a regular file (ENOTDIR, e.g. /somefile/sub). Also when a file is deleted between an exists() check and the stat call (TOCTOU).

Common situations: Cleaning up temp paths that another process already removed; a path typo where a file name is used as a directory component; migration scripts statting paths from an old layout.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/a00f25ce1655d1bc. Report an issue: GitHub.