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
- Verify the name resolves - check getClass().getResource(resource) != null - and mind the leading '/' (absolute) vs none (package-relative).
- List the packaged jar (unzip -l) to confirm the resource was included at the expected path.
- 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
- Use the same resource string for a getResource probe in tests so packaging breaks surface in CI.
- After adding resources, verify the packaged artifact with unzip -l or jar tf.
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
- Can not read resource file '{}'
- webapps/{} not found in CLASSPATH
- readObject can't find class {className}
- Failed to instantiate comparator: {comparator}({e})
- Socket Factory class not found: ${cnfe}
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/bf611bf9a19aafc4.
Report an issue: GitHub.