apache/hadoop · error · InvalidPathException

relative paths not allowed:{path}

Error message

relative paths not allowed:{path}

What it means

AbstractFileSystem.checkPath rejects a Path that has no scheme, no authority, and a non-absolute path part (does not begin with '/'). AbstractFileSystem methods operate on absolute paths bound to that FS instance; FileContext normally qualifies relative paths for you (fixRelativePart + working directory), but direct calls on an AbstractFileSystem bypass that step and hit this InvalidPathException.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java:378

  /**
   * Check that a Path belongs to this FileSystem.
   * 
   * If the path is fully qualified URI, then its scheme and authority
   * matches that of this file system. Otherwise the path must be 
   * slash-relative name.
   * @param path the path.
   * @throws InvalidPathException if the path is invalid
   */
  public void checkPath(Path path) {
    URI uri = path.toUri();
    String thatScheme = uri.getScheme();
    String thatAuthority = uri.getAuthority();
    if (thatScheme == null) {
      if (thatAuthority == null) {
        if (path.isUriPathAbsolute()) {
          return;
        }
        throw new InvalidPathException("relative paths not allowed:" + 
            path);
      } else {
        throw new InvalidPathException(
            "Path without scheme with non-null authority:" + path);
      }
    }
    String thisScheme = this.getUri().getScheme();
    String thisHost = this.getUri().getHost();
    String thatHost = uri.getHost();
    
    // Schemes and hosts must match.
    // Allow for null Authority for file:///
    if (!thisScheme.equalsIgnoreCase(thatScheme) ||
       (thisHost != null && 
            !thisHost.equalsIgnoreCase(thatHost)) ||
       (thisHost == null && thatHost != null)) {
      throw new InvalidPathException("Wrong FS: " + path + ", expected: "
          + this.getUri());

View on GitHub (pinned to 2add963021)

Solutions

  1. Make the path absolute: new Path("/data/file") or prefix with Path.SEPARATOR
  2. Route operations through FileContext, whose fixRelativePart resolves relative paths against the working directory
  3. Qualify the path first: path = path.makeQualified(defaultFS, workingDir)

Example fix

// before
afs.getUriPath(new Path("input/part-0")); // relative paths not allowed

// after
afs.getUriPath(new Path("/user/me/input/part-0"));
Defensive patterns

Strategy: validation

Validate before calling

Path p = input;
if (p.toUri().getScheme() == null && p.toUri().getAuthority() == null && !p.isUriPathAbsolute()) {
  p = new Path(Path.SEPARATOR + p); // or route through FileContext which fixes relative paths
}

Try / catch

catch (InvalidPathException e) { if (e.getMessage().startsWith("relative paths not allowed")) { p = p.makeQualified(defaultFs, wd); /* retry */ } else throw e; }

Prevention

When it happens

Trigger: Calling AbstractFileSystem methods directly (getUriPath, getFileStatus, create, ...) with new Path("data/file"); passing a path built from raw user input that lacks a leading slash; a custom FS framework invoking afs operations without going through FileContext.

Common situations: Unit tests or internal tooling that grab the AbstractFileSystem instead of FileContext; paths read from config files or CLI args without a leading '/'; refactors that replaced FileContext calls with direct AFS calls.

Related errors


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