apache/shenyu · error · FileNotFoundException

File '" + file + "' does not exist

Error message

File '" + file + "' does not exist

What it means

HttpUtils.toBytes(File) throws FileNotFoundException when the given File does not exist at the time of the call. This is a standard Java FileNotFoundException subclass of IOException carrying the path in the message.

Solutions

  1. Correct the configured path to the actual file location; use absolute paths to avoid CWD surprises.
  2. Verify the file exists with Files.exists(path) before calling, and create/obtain it if this is a first-run setup.
  3. In Docker/K8s, confirm the volume or ConfigMap/Secret mounting the file is present in the container.
  4. Catch FileNotFoundException separately from IOException to give a distinct 'file missing' error message.

Example fix

// before
byte[] bytes = HttpUtils.toBytes(new File("certs/server.pem"));
// after
Path path = Paths.get("/etc/shenyu/certs/server.pem");
if (!Files.exists(path)) { throw new IllegalStateException("Missing cert file: " + path); }
byte[] bytes = HttpUtils.toBytes(path.toFile());
Defensive patterns

Strategy: validation

Validate before calling

Path path = file.toPath();
if (!Files.exists(path)) {
    throw new FileNotFoundException("File does not exist: " + path.toAbsolutePath());
}
if (!Files.isRegularFile(path)) {
    throw new IllegalArgumentException("Not a regular file: " + path);
}

Try / catch

try {
    byte[] bytes = HttpUtils.toBytes(file);
} catch (FileNotFoundException e) {
    log.error("Configured file is missing: {}", file, e);
    throw new IllegalStateException("Missing required file — check path and volume mounts", e);
}

Prevention

When it happens

Trigger: Calling toBytes with a path that is absent: a typo in a configured file path, the file was deleted/moved before the call, or an optional file is dereferenced without an existence check.

Common situations: Certificate/keystore paths configured in shenyu-admin pointing to files never created; container volumes not mounted so the expected file is missing; working-directory differences making relative paths resolve incorrectly.

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 apache/shenyu@567142e072 (2026-09-12). Data as JSON: /api/errors/cf6bb5640f2a2bdd. Report an issue: GitHub.

Appendix: source

Thrown at shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java:744

        }

        /**
         * file to bytes.
         *
         * @param file file
         * @return byte
         * @throws IOException IOException
         */
        public static byte[] toBytes(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");
            }
            InputStream input = null;
            try {
                input = Files.newInputStream(file.toPath());
                return toBytes(input);
            } finally {
                try {
                    if (Objects.nonNull(input)) {
                        input.close();
                    }
                } catch (IOException ioe) {
                    LOG.error("toBytes error", ioe);
                }
            }
        }
    }
}

View on GitHub (pinned to 567142e072)