alibaba/spring-cloud-alibaba · error · FileNotFoundException

File '{}' does not exist

Error message

File '{}' does not exist

What it means

Thrown by FileUtils.openInputStream as a FileNotFoundException when the file does not exist on the filesystem at all. This is the first check in the method — if file.exists() is false, it immediately throws rather than attempting to construct a FileInputStream (which would produce a less informative error).

Source

Thrown at spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/io/FileUtils.java:64

	 * but cannot be read.
	 * @param file the file to open for input, must not be {@code null}
	 * @return a new {@link java.io.FileInputStream} for the specified file
	 * @throws java.io.FileNotFoundException if the file does not exist
	 * @throws IOException if the file object is a directory
	 * @throws IOException if the file cannot be read
	 * @since 1.3
	 */
	public static FileInputStream openInputStream(final File file) throws IOException {
		if (file.exists()) {
			if (file.isDirectory()) {
				throw new IOException("File '" + file + "' exists but is a directory");
			}
			if (!file.canRead()) {
				throw new IOException("File '" + file + "' cannot be read");
			}
		}
		else {
			throw new FileNotFoundException("File '" + file + "' does not exist");
		}
		return new FileInputStream(file);
	}

	// -----------------------------------------------------------------------
	/**
	 * Reads the contents of a file into a String. The file is always closed.
	 * @param file the file to read, must not be {@code null}
	 * @param encoding the encoding to use, {@code null} means platform default
	 * @return the file contents, never {@code null}
	 * @throws IOException in case of an I/O error
	 */
	public static String readFileToString(final File file, final Charset encoding)
			throws IOException {
		try (InputStream in = openInputStream(file)) {
			return IOUtils.toString(in, Charsets.toCharset(encoding));
		}
	}

View on GitHub (pinned to 115d590110)

Solutions

  1. Verify the file exists at the specified absolute path: ls -l <path>.
  2. If using a relative path, check the JVM working directory or use an absolute path / Spring ResourceUtils.getResource() for classpath resources.
  3. If the file is supposed to be generated, ensure the generation step ran before this code executes.
  4. If running in a container, verify the volume mount and that the file is present in the image or mounted volume.

Example fix

// before
FileUtils.openInputStream(new File("config/rules.json")); // wrong relative path

// after
FileUtils.openInputStream(new File("/opt/app/config/rules.json")); // absolute path
// or for classpath resources:
FileUtils.openInputStream(ResourceUtils.getFile("classpath:rules.json"));
Defensive patterns

Strategy: validation

Validate before calling

File file = new File(path);
if (!file.exists()) {
    throw new FileNotFoundException(
        "File not found: " + file.getAbsolutePath()
        + " (working dir: " + System.getProperty("user.dir") + ")");
}
FileInputStream fis = FileUtils.openInputStream(file);

Type guard

import java.io.File;

boolean fileExists(File file) {
    return file != null && file.exists() && file.isFile();
}

Try / catch

try {
    InputStream in = FileUtils.openInputStream(file);
} catch (FileNotFoundException e) {
    log.error("File not found: {} — check path and mount points", file.getAbsolutePath());
    throw e;
}

Prevention

When it happens

Trigger: Calling FileUtils.openInputStream(file) (or readFileToString) where the path does not resolve to any existing filesystem entry. Common when a file path is configured but the file was never created, was deleted, or is on a mount point that is not attached.

Common situations: 1) Configured file path points to a location that does not exist (typo, wrong directory). 2) A volume mount in Kubernetes/Docker is not attached, so the path is empty. 3) A file generated by a previous build step is missing in the deployed artifact. 4) Relative path resolved against an unexpected working directory.

Related errors


AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14). Data as JSON: /api/errors/f4e3c0c407f1779e. Report an issue: GitHub.