apache/hadoop · error · FileNotFoundException

${resource}

Error message

${resource}

What it means

fromResource(String) reads JSON from the classpath via getResourceAsStream; when the lookup returns null the resource is not present under that name on any classpath element, and it throws FileNotFoundException carrying the raw resource string. The pre-check separates 'resource not on classpath' from stream read or parse errors.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JsonSerialization.java:220

      IOException {
    writeJsonAsBytes(instance, Files.newOutputStream(file.toPath()));
  }

  /**
   * Convert from a JSON file.
   * @param resource input file
   * @return the parsed JSON
   * @throws IOException IO problems
   * @throws JsonParseException If the input is not well-formatted
   * @throws JsonMappingException failure to map from the JSON to this class
   */
  @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
  public synchronized T fromResource(String resource)
      throws IOException, JsonParseException, JsonMappingException {
    try (InputStream resStream = this.getClass()
        .getResourceAsStream(resource)) {
      if (resStream == null) {
        throw new FileNotFoundException(resource);
      }
      return mapper.readValue(resStream, classType);
    } catch (IOException e) {
      LOG.error("Exception while parsing json resource {}", resource, e);
      throw e;
    }
  }

  /**
   * clone by converting to JSON and back again.
   * This is much less efficient than any Java clone process.
   * @param instance instance to duplicate
   * @return a new instance
   * @throws IOException IO problems.
   */
  public T fromInstance(T instance) throws IOException {
    return fromJson(toJson(instance));
  }

View on GitHub (pinned to 2add963021)

Solutions

  1. Verify the name resolves - check getClass().getResource(resource) != null - and mind the leading '/' (absolute) vs none (package-relative).
  2. List the packaged jar (unzip -l) to confirm the resource was included at the expected path.
  3. Fix the build (shade filters / resource includes) or move the resource into a module that ships on the runtime classpath.

Example fix

// before
MyType t = serializer.fromResource("/schema.json"); // may not be in the jar

// after
try (InputStream in = getClass().getResourceAsStream("/schema.json")) {
  if (in == null) {
    throw new IllegalStateException("schema.json missing from classpath/jar");
  }
  ...
}
Defensive patterns

Strategy: validation

Validate before calling

if (getClass().getResource(resource) == null) {
  throw new IllegalStateException(
      "Resource not on classpath: " + resource + " - check jar contents");
}
return serializer.fromResource(resource);

Prevention

When it happens

Trigger: fromResource("/schema.json") when no schema.json exists at that classpath location: wrong leading-slash convention (absolute vs package-relative), case mismatch, the resource being filtered out of the packaged jar, or a jar version that dropped it.

Common situations: Shade/fat-jar builds excluding resource files; resource present only on the test classpath; a module refactor moved the resource but not the constant; case-sensitive filesystems exposing a case typo.

Related errors


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