perwendel/spark · error · FileNotFoundException
Resource location [ ] is neither a URL not a well-formed…
Error message
Resource location [${resourceLocation}] is neither a URL not a well-formed file path What it means
ResourceUtils.getURL first tries to interpret the location string as a URL; on MalformedURLException it falls back to treating it as a file path. If both conversions fail — the string is neither a well-formed URL nor a usable file path — it throws FileNotFoundException stating the resource location is 'neither a URL not a well-formed file path'.
Solutions
- Fix the location string: either a full valid URL (e.g. http://..., file:/absolute/path) or a valid file path.
- Encode special characters (spaces -> %20) or drop an invalid scheme and pass the plain absolute file path.
- On Windows use forward slashes or the file:/// form with proper drive-letter syntax (file:/C:/temp/x.txt).
- Validate the location with new URL(...) or new File(...).toURI().toURL() in a test before wiring it into config.
Example fix
// before
URL url = ResourceUtils.getURL("file://C:\\temp\\my file.txt"); // malformed
// after
URL url = ResourceUtils.getURL("file:/C:/temp/my%20file.txt"); Defensive patterns
Strategy: validation
Validate before calling
try {
new URL(location);
} catch (MalformedURLException e) {
try { new File(location).toURI().toURL(); } catch (MalformedURLException e2) {
throw new IllegalStateException("Location is neither URL nor valid file path: " + location);
}
} Try / catch
try {
URL url = ResourceUtils.getURL(location);
} catch (FileNotFoundException e) {
log.error("Malformed resource location: {}", location);
} Prevention
- Encode spaces and special characters in paths (%20).
- On Windows prefer forward slashes and proper file:/ URLs.
- Validate configured locations at startup with a fail-fast check.
When it happens
Trigger: Calling getURL with a malformed string such as "htp:/bad", "file://C:temp" (invalid for toURI().toURL()), a string with illegal URI characters (spaces/control chars not encoded), or a location with a wrong scheme prefix that is neither classpath:, http(s), nor a valid path.
Common situations: Windows paths with backslashes or drive letters combined with a file:// prefix the URI parser rejects; unencoded spaces in file paths; config values concatenated incorrectly producing invalid URLs; typos in protocol names.
Understand the failure class
Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.
Related errors
- WebSocket handler must implement 'WebSocketListener' or be…
- path cannot be null or blank
- httpMethod cannot be null or blank
- The must start with "/" and end with "/*". It's
- There are no Spark applications configured in the filter.
AI-assisted analysis of perwendel/spark@1973e402f5 (2026-09-10).
Data as JSON: /api/errors/91faee341203adb5.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/spark/utils/ResourceUtils.java:145
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");
}
}
}
/**
* 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 {View on GitHub (pinned to 1973e402f5)