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: ${resourceUrl}

What it means

ResourceUtils.getFile(URL, String) converts a URL into a java.io.File, but only when the URL protocol is 'file'. Any other protocol (jar:, http:, etc.) means the resource does not reside on the file system, so a File cannot be produced and FileNotFoundException is thrown.

Solutions

  1. Check resourceUrl.getProtocol() before calling getFile and fall back to stream access when it is not 'file'.
  2. Read the resource via url.openStream() or getResourceAsStream instead of File.
  3. If the resource must be a real File, unpack it from the jar to a temp directory first (e.g. Files.copy to a temp file).
  4. Run from an exploded classpath (classes directory) during development to confirm the resource resolves as file:.

Example fix

// before
File f = ResourceUtils.getFile(url, "config");
// after
if ("file".equals(url.getProtocol())) {
    File f = ResourceUtils.getFile(url, "config");
} else {
    try (InputStream in = url.openStream()) { /* read stream */ }
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (url == null || !"file".equals(url.getProtocol()))
    throw new IllegalArgumentException("Not a file-system URL: " + url);

Type guard

boolean isFileUrl(URL u) { return u != null && "file".equals(u.getProtocol()); }

Try / catch

try {
    File f = ResourceUtils.getFile(url, "config");
} catch (FileNotFoundException e) {
    try (InputStream in = url.openStream()) { /* read from jar */ }
}

Prevention

When it happens

Trigger: Passing a URL obtained from getResource()/getSystemResource() whose protocol is not 'file' — most commonly a jar: URL for a resource packaged inside a jar — into ResourceUtils.getFile(URL, description).

Common situations: Application deployed as a fat/uber jar where classpath resources are inside the archive; resource served from a remote location; code that works from IDE classes dir but fails in packaged jar.

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

Appendix: source

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

    public static File getFile(URL resourceUrl) throws FileNotFoundException {
        return getFile(resourceUrl, "URL");
    }

    /**
     * Resolve the given resource URL to a {@code java.io.File},
     * i.e. to a file in the file system.
     *
     * @param resourceUrl the resource URL to resolve
     * @param description a description of the original resource that
     *                    the URL was created for (for example, a class path location)
     * @return a corresponding File object
     * @throws FileNotFoundException if the URL cannot be resolved to
     *                               a file in the file system
     */
    public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
        Assert.notNull(resourceUrl, "Resource URL must not be null");
        if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
            throw new FileNotFoundException(
                    description + " cannot be resolved to absolute file path " +
                            "because it does not reside in the file system: " + resourceUrl
            );
        }
        try {
            return new File(toURI(resourceUrl).getSchemeSpecificPart());
        } catch (URISyntaxException ex) {
            // Fallback for URLs that are not valid URIs (should hardly ever happen).
            return new File(resourceUrl.getFile());
        }
    }

    /**
     * Resolve the given resource URI to a {@code java.io.File},
     * i.e. to a file in the file system.
     *
     * @param resourceUri the resource URI to resolve
     * @return a corresponding File object

View on GitHub (pinned to 1973e402f5)