apache/hadoop · error · IOException

Can not read resource file '{}' because given class loader i

Error message

Can not read resource file '{}' because given class loader is null

What it means

The two-arg ThreadUtil.getResourceAsStream(ClassLoader, String) requires a non-null classloader; passing null throws this IOException immediately. Code reaches this with an explicitly passed null, a never-initialized loader field, or via the one-arg overload when the thread context classloader is null (which forwards the same null here).

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java:116

    return getResourceAsStream(cl, resourceName);
  }

  /**
   * Convenience method that returns a resource as inputstream from the
   * classpath using given classloader.
   * <p>
   *
   * @param cl ClassLoader to be used to retrieve resource.
   * @param resourceName resource to retrieve.
   *
   * @throws IOException thrown if resource cannot be loaded
   * @return inputstream with the resource.
   */
  public static InputStream getResourceAsStream(ClassLoader cl,
        String resourceName)
        throws IOException {
    if (cl == null) {
      throw new IOException("Can not read resource file '" + resourceName +
          "' because given class loader is null");
    }
    InputStream is = cl.getResourceAsStream(resourceName);
    if (is == null) {
      throw new IOException("Can not read resource file '" +
          resourceName + "'");
    }
    return is;
  }


}

View on GitHub (pinned to 2add963021)

Solutions

  1. Pass a real loader: getClass().getClassLoader(), or the TCCL when known non-null.
  2. Null-check the loader before the call and pick a fallback loader (system classloader or the class's own).
  3. Fix the one-arg call path (error 1845) if this surfaces indirectly.
  4. Catch IOException around resource loading to degrade features that need the resource.

Example fix

// before
ClassLoader cl = resolveLoader(); // returns null
InputStream is = ThreadUtil.getResourceAsStream(cl, "a.xml");
// throws: given class loader is null

// after
ClassLoader cl = resolveLoader();
if (cl == null) cl = MyApp.class.getClassLoader();
InputStream is = ThreadUtil.getResourceAsStream(cl, "a.xml");
Defensive patterns

Strategy: validation

Validate before calling

if (cl == null) {
  cl = MyApp.class.getClassLoader();
}
InputStream is = ThreadUtil.getResourceAsStream(cl, resourceName);

Try / catch

try {
  is = ThreadUtil.getResourceAsStream(cl, resourceName);
} catch (IOException e) {
  LOG.error("Cannot load resource " + resourceName + " via " + cl, e);
  throw e;
}

Prevention

When it happens

Trigger: ThreadUtil.getResourceAsStream(null, "a.xml"); calling with a ClassLoader field that was never assigned; a loader obtained from a detached context that legitimately returns null; chained from error 1845's one-arg variant.

Common situations: Optional-dependency code that resolves a loader reflectively and gets null; passing Thread.currentThread().getContextClassLoader() without a null check; refactor that dropped the loader initialization but kept the call.

Related errors


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