perwendel/spark · error · FileNotFoundException

class path resource [ ] cannot be resolved to URL because…

Error message

class path resource [${path}] cannot be resolved to URL because it does not exist

What it means

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'.

Solutions

  1. Verify the resource exists at that exact classpath path: check src/main/resources and the built JAR (jar tf app.jar | grep <path>).
  2. Fix the path spelling — after 'classpath:' the path is relative to the classpath root; remove erroneous leading/trailing slashes.
  3. Ensure the file is included by the build (Maven/Gradle resource filtering/excludes).
  4. If the resource is optional, check existence first via ClassUtils.getDefaultClassLoader().getResource(path) before calling getURL.

Example fix

// before
URL url = ResourceUtils.getURL("classpath:confg/app.yml"); // typo -> FileNotFoundException

// after
URL url = ResourceUtils.getURL("classpath:config/app.yml");
Defensive patterns

Strategy: try-catch

Validate before calling

java.net.URL check = ClassUtils.getDefaultClassLoader().getResource("config/app.yml");
if (check == null) throw new IllegalStateException("classpath resource config/app.yml missing");

Try / catch

try {
    URL url = ResourceUtils.getURL(location);
} catch (FileNotFoundException e) {
    log.error("Classpath resource missing: {}", e.getMessage());
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of perwendel/spark@1973e402f5 (2026-09-10). Data as JSON: /api/errors/c3f020814cfa56bd. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/spark/utils/ResourceUtils.java:132

    /**
     * Resolve the given resource location to a {@code java.net.URL}.
     * <p>Does not check whether the URL actually exists; simply returns
     * the URL that the given location would correspond to.
     *
     * @param resourceLocation the resource location to resolve: either a
     *                         "classpath:" pseudo URL, a "file:" URL, or a plain file path
     * @return a corresponding URL object
     * @throws FileNotFoundException if the resource cannot be resolved to a URL
     */
    public static URL getURL(String resourceLocation) throws FileNotFoundException {
        Assert.notNull(resourceLocation, "Resource location must not be null");
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
            URL url = ClassUtils.getDefaultClassLoader().getResource(path);
            if (url == null) {
                String description = "class path resource [" + path + "]";
                throw new FileNotFoundException(
                        description + " cannot be resolved to URL because it does not exist");
            }
            return url;
        }
        try {
            // try URL
            return new URL(resourceLocation);
        } catch (MalformedURLException ex) {
            // no URL -> treat as file path
            try {
                return new File(resourceLocation).toURI().toURL();
            } catch (MalformedURLException ex2) {
                throw new FileNotFoundException("Resource location [" + resourceLocation +
                                                        "] is neither a URL not a well-formed file path");
            }
        }
    }

View on GitHub (pinned to 1973e402f5)