{"record":{"id":"18266ef44bf3e9f6","repo":"apache/druid","slug":"cannot-load-resource-s","errorCode":null,"errorMessage":"Cannot load resource: [%s]","messagePattern":"Cannot load resource: \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java","lineNumber":777,"sourceCode":"  {\n    if (s == null || s.length() <= maxBytes) {\n      return s;\n    } else {\n      return s.substring(0, maxBytes);\n    }\n  }\n\n  public static String getResource(Object ref, String resource)\n  {\n    try {\n      InputStream is = ref.getClass().getResourceAsStream(resource);\n      if (is == null) {\n        throw new ISE(\"Resource not found: [%s]\", resource);\n      }\n      return IOUtils.toString(is, StandardCharsets.UTF_8);\n    }\n    catch (IOException e) {\n      throw new ISE(e, \"Cannot load resource: [%s]\", resource);\n    }\n  }\n\n  /**\n   This method is removed from commons lang3.\n   https://commons.apache.org/proper/commons-lang/article3_0.html\n   */\n  public static String escapeSql(String str)\n  {\n    return str == null ? null : StringUtils.replace(str, \"'\", \"''\");\n  }\n\n  /**\n   * Uses {@link StringEscapeUtils#escapeHtml4} to escape the HTML entities in\n   * the given string. This method can be used to sanitize a string for XSS.\n   */\n  public static String escapeHtml(String value)\n  {","sourceCodeStart":759,"sourceCodeEnd":795,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/StringUtils.java#L759-L795","documentation":"StringUtils.getResource(Object ref, String resource) reads the classpath resource with IOUtils.toString; if reading throws an IOException (I/O error while reading, stream closed/broken), it wraps it in IllegalStateException \"Cannot load resource: [%s]\". This is distinct from the resource being absent (which gives \"Resource not found\").","triggerScenarios":"Calling StringUtils.getResource(ref, path) when the underlying stream fails mid-read — e.g. a corrupted jar, an I/O error on a filesystem-backed classpath entry, or a stream closed by another component.","commonSituations":"Damaged or partially-downloaded jars in deployment; container/classloader issues reading nested resources; disk errors on shared filesystems.","solutions":["Verify the jar/classpath entry is intact (checksum, redeploy the artifact).","Rebuild and redeploy the application to replace a corrupted artifact.","Inspect the wrapped cause (e.getCause()) to identify the underlying IOException source.","Catch IllegalStateException around getResource and fall back to a copy of the resource bundled elsewhere."],"exampleFix":"// before\nString tpl = StringUtils.getResource(Main.class, \"/templates/x.sql\");\n// after\nString tpl;\ntry {\n  tpl = StringUtils.getResource(Main.class, \"/templates/x.sql\");\n} catch (IllegalStateException e) {\n  LOG.error(e, \"Failed to load resource; using default\");\n  tpl = DEFAULT_TEMPLATE;\n}","handlingStrategy":"try-catch","validationCode":"try (InputStream is = ref.getClass().getResourceAsStream(resource)) { if (is != null) { is.readAllBytes(); } } catch (IOException e) { /* resource unreadable — handle before calling */ }","typeGuard":null,"tryCatchPattern":"try {\n  return StringUtils.getResource(ref, resource);\n} catch (IllegalStateException e) {\n  LOG.error(e.getCause(), \"Cannot load resource %s\", resource);\n  throw e;\n}","preventionTips":["Verify artifact integrity (checksums) when deployments come from shared storage","Inspect getCause() of the ISE to find the underlying IOException","Keep fallback copies of critical resources outside the jar if the classpath is unreliable"],"tags":["classpath","resource-loading","io-error"],"backgroundTag":"file-read-failed","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}