apache/dubbo · error · FileNotFoundException

Resource location [{resourceLocation}] is neither a URL not

Error message

Resource location [{resourceLocation}] is neither a URL not a well-formed file path

What it means

IOUtils.getURL treats a non-classpath location first as a URL, then as a file path. If both new URL(...) and new File(...).toURI().toURL() throw MalformedURLException, the location is neither a valid URL nor a usable file path and FileNotFoundException is thrown.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java:274

        if (resourceLocation.startsWith(CommonConstants.CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CommonConstants.CLASSPATH_URL_PREFIX.length());
            ClassLoader cl = ClassUtils.getClassLoader();
            URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(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");
            }
        }
    }

    public static byte[] toByteArray(final InputStream inputStream) throws IOException {
        try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int n;
            while (-1 != (n = inputStream.read(buffer))) {
                byteArrayOutputStream.write(buffer, 0, n);
            }
            return byteArrayOutputStream.toByteArray();
        }
    }
}

View on GitHub (pinned to 3a3043227f)

Solutions

  1. URL-encode the location or pass a proper file:/http: URL
  2. For local files, pass an absolute OS path that exists, or convert via new File(path).toURI().toURL() yourself first
  3. Prefix classpath resources with the classpath: scheme handled earlier in the method

Example fix

// before
URL u = IOUtils.getURL("C:\\my dir\\file.txt");
// after
URL u = IOUtils.getURL(new File("C:\\my dir\\file.txt").toURI().toString());
Defensive patterns

Strategy: validation

Validate before calling

String loc = ...;
try { new java.net.URL(loc); /* ok */ }
catch (java.net.MalformedURLException e) {
    java.io.File f = new java.io.File(loc);
    if (!f.exists()) { /* will fail in getURL; resolve first */ }
}

Type guard

static boolean isUrlOrFilePath(String s) {
    try { new java.net.URL(s); return true; }
    catch (Exception e) { return new java.io.File(s).exists(); }
}

Try / catch

try { URL u = IOUtils.getURL(loc); }
catch (java.io.FileNotFoundException e) { /* neither URL nor file; encode or fix path */ }

Prevention

When it happens

Trigger: Passing a malformed string that is not a classpath prefix, not a parseable URL (e.g. contains illegal characters/spaces without encoding), and not an existing/convertible file path.

Common situations: Unclosed URL protocols, Windows paths with backslashes, strings with spaces or special characters, or a path that does not exist on disk and cannot be expressed as a URL.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/48daffa5335dedcc. Report an issue: GitHub.