{"record":{"id":"c3f020814cfa56bd","repo":"perwendel/spark","slug":"class-path-resource-path-cannot-be-resolved-t","errorCode":null,"errorMessage":"class path resource [${path}] cannot be resolved to URL because it does not exist","messagePattern":"class path resource \\[(.+?)\\] cannot be resolved to URL because it does not exist","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"src/main/java/spark/utils/ResourceUtils.java","lineNumber":132,"sourceCode":"\n    /**\n     * Resolve the given resource location to a {@code java.net.URL}.\n     * <p>Does not check whether the URL actually exists; simply returns\n     * the URL that the given location would correspond to.\n     *\n     * @param resourceLocation the resource location to resolve: either a\n     *                         \"classpath:\" pseudo URL, a \"file:\" URL, or a plain file path\n     * @return a corresponding URL object\n     * @throws FileNotFoundException if the resource cannot be resolved to a URL\n     */\n    public static URL getURL(String resourceLocation) throws FileNotFoundException {\n        Assert.notNull(resourceLocation, \"Resource location must not be null\");\n        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {\n            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());\n            URL url = ClassUtils.getDefaultClassLoader().getResource(path);\n            if (url == null) {\n                String description = \"class path resource [\" + path + \"]\";\n                throw new FileNotFoundException(\n                        description + \" cannot be resolved to URL because it does not exist\");\n            }\n            return url;\n        }\n        try {\n            // try URL\n            return new URL(resourceLocation);\n        } catch (MalformedURLException ex) {\n            // no URL -> treat as file path\n            try {\n                return new File(resourceLocation).toURI().toURL();\n            } catch (MalformedURLException ex2) {\n                throw new FileNotFoundException(\"Resource location [\" + resourceLocation +\n                                                        \"] is neither a URL not a well-formed file path\");\n            }\n        }\n    }\n","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/perwendel/spark/blob/1973e402f5d4c1442ad34a1d38ed0758079f7773/src/main/java/spark/utils/ResourceUtils.java#L114-L150","documentation":"ResourceUtils.getURL resolves a resource location string to a java.net.URL. For locations starting with the \"classpath:\" prefix it loads the path via the default class loader; if the resource does not exist on the classpath, the lookup returns null and Spark throws FileNotFoundException with the message 'class path resource [<path>] cannot be resolved to URL because it does not exist'.","triggerScenarios":"Calling getURL(\"classpath:some/resource.xml\") (or passing such a location to APIs built on ResourceUtils) where the named file is absent from the classpath — wrong package path, missing resource in the JAR, or a typo in the path (classpath: paths are absolute; no leading slash is expected after the prefix).","commonSituations":"Resource present in src/main/resources but the build excluded it or the app is run from a directory that lacks it; typo like classpath:/config/app.yml double slash; resource only available in a test fixture, not in the production artifact; classloader differences in servlet containers.","solutions":["Verify the resource exists at that exact classpath path: check src/main/resources and the built JAR (jar tf app.jar | grep <path>).","Fix the path spelling — after 'classpath:' the path is relative to the classpath root; remove erroneous leading/trailing slashes.","Ensure the file is included by the build (Maven/Gradle resource filtering/excludes).","If the resource is optional, check existence first via ClassUtils.getDefaultClassLoader().getResource(path) before calling getURL."],"exampleFix":"// before\nURL url = ResourceUtils.getURL(\"classpath:confg/app.yml\"); // typo -> FileNotFoundException\n\n// after\nURL url = ResourceUtils.getURL(\"classpath:config/app.yml\");","handlingStrategy":"try-catch","validationCode":"java.net.URL check = ClassUtils.getDefaultClassLoader().getResource(\"config/app.yml\");\nif (check == null) throw new IllegalStateException(\"classpath resource config/app.yml missing\");","typeGuard":null,"tryCatchPattern":"try {\n    URL url = ResourceUtils.getURL(location);\n} catch (FileNotFoundException e) {\n    log.error(\"Classpath resource missing: {}\", e.getMessage());\n}","preventionTips":["Verify resources exist in the built artifact (jar tf) in CI.","Remember classpath: paths are absolute from the classpath root — no extra slashes.","Check build resource includes/excludes when a resource works locally but not in the JAR."],"tags":["classpath","resource","file-not-found","spark"],"backgroundTag":"file-not-found","analyzedSha":"1973e402f5d4c1442ad34a1d38ed0758079f7773","analyzedAt":"2026-09-10T14:38:22.866Z","contentChangedAt":"2026-09-10T14:38:22.866Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}