perwendel/spark · error · java.io.FileNotFoundException

cannot be resolved to absolute file path because it does…

Error message

${description} cannot be resolved to absolute file path because it does not reside in the file system

What it means

ResourceUtils.getFile(String) resolves a classpath-prefixed ('classpath:') location to an absolute java.io.File. It throws FileNotFoundException when the resource cannot be found on the classpath, and the message also covers the case where a resource exists only inside a jar/zip and therefore has no file-system path.

Solutions

  1. Verify the path after 'classpath:' matches a file actually present on the runtime classpath (check target/classes or the jar contents).
  2. If the resource lives inside a jar, read it as a stream (ClassUtils.getDefaultClassLoader().getResourceAsStream(path)) instead of a File.
  3. Use ClassLoader.getResource(path) yourself first and inspect the URL protocol to confirm it is 'file'.
  4. Fix build configuration so the resources directory is included on the classpath.

Example fix

// before
File f = ResourceUtils.getFile("classpath:config/app.properties");
// after
InputStream in = ClassUtils.getDefaultClassLoader().getResourceAsStream("config/app.properties");
if (in == null) throw new FileNotFoundException("config/app.properties not on classpath");
Defensive patterns

Strategy: validation

Validate before calling

String path = location.substring("classpath:".length());
if (ClassUtils.getDefaultClassLoader().getResource(path) == null)
    throw new IllegalStateException("Resource not on classpath: " + path);

Try / catch

try {
    File f = ResourceUtils.getFile("classpath:app.properties");
} catch (FileNotFoundException e) {
    // fall back to stream access from the jar
    try (InputStream in = getClass().getResourceAsStream("/app.properties")) { ... }
}

Prevention

When it happens

Trigger: Calling ResourceUtils.getFile("classpath:some/file.properties") when the path is not on the classpath (typo, missing resource, wrong module), or when getResource returns a jar: URL instead of a file: URL because the resource is packaged in a jar.

Common situations: Forgetting to put a resource folder on the test classpath; renaming/moving a resource; expecting a classpath resource inside a fat jar to be readable as a File (it never is); resource excluded by build filters.

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/3b8e4e4e61036faf. Report an issue: GitHub.

Appendix: source

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

     * Resolve the given resource location to a {@code java.io.File},
     * i.e. to a file in the file system.
     * <p>Does not check whether the file actually exists; simply returns
     * the File 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 File object
     * @throws FileNotFoundException if the resource cannot be resolved to
     *                               a file in the file system
     */
    public static File getFile(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());
            String description = "class path resource [" + path + "]";
            URL url = ClassUtils.getDefaultClassLoader().getResource(path);
            if (url == null) {
                throw new FileNotFoundException(
                        description + " cannot be resolved to absolute file path " +
                                "because it does not reside in the file system"
                );
            }
            return getFile(url, description);
        }
        try {
            // try URL
            return getFile(new URL(resourceLocation));
        } catch (MalformedURLException ex) {
            // no URL -> treat as file path
            return new File(resourceLocation);
        }
    }

    /**
     * Resolve the given resource URL to a {@code java.io.File},
     * i.e. to a file in the file system.

View on GitHub (pinned to 1973e402f5)