alibaba/nacos · error · FileNotFoundException

{getDescription()} cannot be resolved to absolute file path

Error message

{getDescription()} cannot be resolved to absolute file path

What it means

Default AbstractResource.getFile() throws FileNotFoundException when a concrete subclass has not overridden getFile() to return a java.io.File. It is the base fallback: resources that are not file-backed cannot be resolved to an absolute file path. Hit when code calls getFile() on a non-file resource (e.g. a classpath-in-jar or URL resource).

Source

Thrown at common/src/main/java/com/alibaba/nacos/common/packagescan/resource/AbstractResource.java:134

     * by {@link #getUrl()}.
     */
    @Override
    public URI getUri() throws IOException {
        URL url = getUrl();
        try {
            return ResourceUtils.toUri(url);
        } catch (URISyntaxException ex) {
            throw new NestedIoException("Invalid URI [" + url + "]", ex);
        }
    }

    /**
     * This implementation throws a FileNotFoundException, assuming
     * that the resource cannot be resolved to an absolute file path.
     */
    @Override
    public File getFile() throws IOException {
        throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
    }

    /**
     * This implementation returns {@link Channels#newChannel(InputStream)}
     * with the result of {@link #getInputStream()}.
     * This is the same as in {@link Resource}'s corresponding default method
     * but mirrored here for efficient JVM-level dispatching in a class hierarchy.
     */
    @Override
    public ReadableByteChannel readableChannel() throws IOException {
        return Channels.newChannel(getInputStream());
    }

    /**
     * This method reads the entire InputStream to determine the content length.
     * For a custom sub-class of {@code InputStreamResource}, we strongly
     * recommend overriding this method with a more optimal implementation, e.g.
     * checking File length, or possibly simply returning -1 if the stream can

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Use getInputStream() instead of getFile() for non-file-backed resources (works inside jars).
  2. If you truly need a File, copy the stream to a temp file via Files.copy(in, tmp).
  3. Override getFile() in your subclass to return a real File when the resource is file-backed.
  4. Use ClassPathResource only when you know the resource is on the filesystem, not inside a jar.

Example fix

// before
File f = resource.getFile(); // FileNotFoundException for jar classpath entry

// after
File tmp = File.createTempFile("res", ".tmp");
try (InputStream in = resource.getInputStream()) {
    Files.copy(in, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
File f = tmp;
Defensive patterns

Strategy: validation

Validate before calling

File f;
try {
    f = resource.getFile();
} catch (FileNotFoundException e) {
    // not file-backed; stream to a temp file if a File is truly required
    f = Files.createTempFile("res", ".tmp").toFile();
    try (InputStream in = resource.getInputStream()) {
        Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
}

Type guard

boolean isFileBacked(Resource r) {
    try { return r.getFile() != null; }
    catch (IOException e) { return false; }
}

Try / catch

try {
    File f = resource.getFile();
} catch (FileNotFoundException e) {
    log.debug("Resource not file-backed, using stream");
}

Prevention

When it happens

Trigger: Calling resource.getFile() on an AbstractResource subclass that only provides getInputStream/getUrl; resolving a classpath resource that lives inside a jar to a File; a custom resource with no file representation.

Common situations: Classpath resource packaged in a jar (getFile() cannot return a real File for a jar entry); URL/byte-array resources passed to code expecting a File; tests stubbing AbstractResource without overriding getFile().

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/c0f7bb78f1ba7210. Report an issue: GitHub.