{"record":{"id":"4d8b8b8b5b822814","repo":"mybatis/mybatis-3","slug":"could-not-find-resource","errorCode":null,"errorMessage":"Could not find resource {}","messagePattern":"Could not find resource (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/apache/ibatis/io/Resources.java","lineNumber":98,"sourceCode":"  }\n\n  /**\n   * Returns the URL of the resource on the classpath\n   *\n   * @param loader\n   *          The classloader used to fetch the resource\n   * @param resource\n   *          The resource to find\n   *\n   * @return The resource\n   *\n   * @throws java.io.IOException\n   *           If the resource cannot be found or read\n   */\n  public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {\n    URL url = classLoaderWrapper.getResourceAsURL(resource, loader);\n    if (url == null) {\n      throw new IOException(\"Could not find resource \" + resource);\n    }\n    return url;\n  }\n\n  /**\n   * Returns a resource on the classpath as a Stream object\n   *\n   * @param resource\n   *          The resource to find\n   *\n   * @return The resource\n   *\n   * @throws java.io.IOException\n   *           If the resource cannot be found or read\n   */\n  public static InputStream getResourceAsStream(String resource) throws IOException {\n    return getResourceAsStream(null, resource);\n  }","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/mybatis/mybatis-3/blob/008069adb1b089579b5dcba87ee591908b263274/src/main/java/org/apache/ibatis/io/Resources.java#L80-L116","documentation":"Resources.getResourceURL asked ClassLoaderWrapper for a classpath resource across the full classloader chain and every loader returned null, so it throws IOException 'Could not find resource'. This is the URL variant used when a real URL (not a stream) is required, e.g. by the XML config/mapper loaders.","triggerScenarios":"SqlSessionFactoryBuilder.build(reader) is absent — instead config_LOCATION style APIs: Resources.getResourceURL(\"mybatis-config.xml\") or mybatis-config loader resolving <mapper resource=\"...\"> / <sqlMap ...> entries where the path is wrong relative to the classpath root.","commonSituations":"Leading slash or 'src/main/resources' prefix in the path; file placed under a package instead of the root; resource in a jar not on the runtime classpath; case-sensitivity on Linux; IDE build not copying resources; webapp deployments with non-standard classloaders.","solutions":["Use a classpath-root-relative path without leading slash: 'mybatis-config.xml', 'mappers/UserMapper.xml' — never 'src/main/resources/...'.","Verify the file is in target/classes (or the deployed jar) after a clean build.","Check path case exactly matches the file name on case-sensitive filesystems.","Pass an explicit ClassLoader (Resources.getResourceURL(loader, path)) in container/boot environments where the context loader cannot see app classes."],"exampleFix":"// before\nURL url = Resources.getResourceURL(\"src/main/resources/mybatis-config.xml\");\n\n// after\nURL url = Resources.getResourceURL(\"mybatis-config.xml\");","handlingStrategy":"validation","validationCode":"// verify resource existence with a clear error before MyBatis loads it\nstatic URL requireResource(ClassLoader cl, String path) {\n  URL url = cl == null ? Thread.currentThread().getContextClassLoader().getResource(path) : cl.getResource(path);\n  if (url == null) throw new IllegalStateException(\"Missing classpath resource (expected at classpath root, no leading '/'): \" + path);\n  return url;\n}","typeGuard":"static boolean resourceExists(ClassLoader cl, String path) {\n  return (cl != null ? cl : Thread.currentThread().getContextClassLoader()).getResource(path) != null;\n}","tryCatchPattern":null,"preventionTips":["Reference resources classpath-root-relative without leading slashes or src/main/resources prefixes.","Add a bootstrap assertion that key config/mapper resources resolve, so packaging mistakes fail at startup."],"tags":["mybatis","resources","classpath","configuration"],"backgroundTag":null,"analyzedSha":"008069adb1b089579b5dcba87ee591908b263274","analyzedAt":"2026-08-14T13:07:10.264Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}