{"record":{"id":"e6d7cf852e104118","repo":"apache/hadoop","slug":"can-not-read-resource-file","errorCode":null,"errorMessage":"Can not read resource file '{}'","messagePattern":"Can not read resource file '(.+?)'","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java","lineNumber":121,"sourceCode":"   * classpath using given classloader.\n   * <p>\n   *\n   * @param cl ClassLoader to be used to retrieve resource.\n   * @param resourceName resource to retrieve.\n   *\n   * @throws IOException thrown if resource cannot be loaded\n   * @return inputstream with the resource.\n   */\n  public static InputStream getResourceAsStream(ClassLoader cl,\n        String resourceName)\n        throws IOException {\n    if (cl == null) {\n      throw new IOException(\"Can not read resource file '\" + resourceName +\n          \"' because given class loader is null\");\n    }\n    InputStream is = cl.getResourceAsStream(resourceName);\n    if (is == null) {\n      throw new IOException(\"Can not read resource file '\" +\n          resourceName + \"'\");\n    }\n    return is;\n  }\n\n\n}\n","sourceCodeStart":103,"sourceCodeEnd":129,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java#L103-L129","documentation":"After resolving a non-null classloader, ThreadUtil.getResourceAsStream asks it for the named resource; ClassLoader.getResourceAsStream returns null (rather than throwing) when nothing matches at that name on the classpath, and this method converts that null into an IOException. So this error means 'the loader is fine, but no resource exists at that path for it'.","triggerScenarios":"Wrong resource name: 'core-default.xml' when the file lives under 'org/apache/hadoop/' — absolute names need the full package path with or without a leading '/'; resources excluded from the packaged jar; shaded/relocated jars that moved resources; using the TCCL when the resource is only visible to the application classloader.","commonSituations":"src/main/resources file filtered out of the build; running a skinny jar where the resource is in a dependency jar that is missing; typo or case mismatch in the path; Maven shade plugin dropping transformer-handled resources; tests with a classpath that lacks the resource module.","solutions":["Verify the name resolves first: URL u = cl.getResource(resourceName); adjust until it is non-null.","Use the fully-qualified path including package directories, e.g. 'org/apache/hadoop/fs/commons-default.xml' (absolute) or call with getClass().getResource semantics.","Check the packaged artifact (unzip -l app.jar | grep <name>) and fix pom/resource includes if the file was excluded.","Pass the classloader whose classpath actually contains the resource instead of relying on the TCCL."],"exampleFix":"// before\nInputStream is = ThreadUtil.getResourceAsStream(cl, \"core-default.xml\");\n// throws: Can not read resource file 'core-default.xml' (lives under org/apache/hadoop/)\n\n// after\nInputStream is = ThreadUtil.getResourceAsStream(cl,\n    \"org/apache/hadoop/core-default.xml\");","handlingStrategy":"validation","validationCode":"if (cl.getResource(resourceName) == null) {\n  throw new FileNotFoundException(\n      \"Resource '\" + resourceName + \"' not found on \" + cl\n      + \"; expected on the classpath, check packaging\");\n}\nInputStream is = ThreadUtil.getResourceAsStream(cl, resourceName);","typeGuard":null,"tryCatchPattern":"try {\n  is = ThreadUtil.getResourceAsStream(cl, resourceName);\n} catch (IOException e) {\n  // name-resolution miss: try package-qualified variants\n  for (String alt : altResourcePaths(resourceName)) {\n    is = cl.getResourceAsStream(alt);\n    if (is != null) break;\n  }\n  if (is == null) throw e;\n}","preventionTips":["Prove the path with cl.getResource(name) before opening the stream.","Keep resource names in constants derived from the owning class's package.","Verify packaged jars contain the resource (unzip -l) in CI smoke checks."],"tags":["classpath","resources","packaging","hadoop-common"],"backgroundTag":"classpath-resource-not-found","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}