FileNotFoundException
Java Programming Language
Severity: ModerateWhat Does This Error Mean?
A FileNotFoundException means Java looked for a file at the path you gave it and could not find anything there. The file might not exist, the path might be wrong, or the program might be looking in a different folder than you expect. This is a checked exception — Java requires you to handle it because file access can always fail.
Affected Models
- Java 8
- Java 11
- Java 17
- Java 21
- All Java versions
Common Causes
- The file path is incorrect — a typo in the file name, wrong folder, or missing directory
- Relative paths resolve to the wrong location — the path is relative to the working directory, which is not what you expected
- The file exists but the program does not have permission to read or open it
- The path separators are wrong — using backslashes on a system that expects forward slashes, or vice versa
- The file was deleted or moved between the time the program checked for it and tried to open it
How to Fix It
-
Print the full absolute path Java resolves to: System.out.println(new File(yourPath).getAbsolutePath()). Compare that to where the file actually is on disk.
This is the single most useful debugging step. Many developers waste time guessing the path when printing it gives the answer immediately.
-
Check for typos in the file name and path. File names are case-sensitive on Linux and macOS. 'MyFile.txt' and 'myfile.txt' are different files.
On Windows, file names are case-insensitive, so the same code that works on Windows may fail when deployed to a Linux server.
-
Avoid relative paths in production code. Use absolute paths, or load files from the classpath using getClass().getResourceAsStream('/data/file.txt') so the path is relative to your JAR, not the working directory.
getResourceAsStream() is reliable for files bundled inside your JAR or on the classpath. It works regardless of where the user runs your application from.
-
Use File.separator or the Paths API instead of hardcoded slashes. Paths.get('data', 'file.txt') constructs the correct path for the current operating system automatically.
This avoids Windows vs. Linux path separator problems. Paths.get() is the modern, preferred way to build file paths in Java.
-
Before opening a file, check whether it exists and is readable: File f = new File(path); if (f.exists() && f.canRead()) — this gives you a friendly error instead of an exception.
Also check f.isFile() to ensure it is not a directory, and f.canRead() to confirm read permission.
When to Call a Professional
FileNotFoundException is always fixable — it is about where Java looks for the file versus where the file actually is. Print the full absolute path Java is using to confirm. Then compare it to where the file really is.
Frequently Asked Questions
What is the working directory in Java and why does it matter?
The working directory is the folder Java treats as the base when resolving relative file paths. When you run a JAR from the command line, the working directory is wherever you ran the command from. In an IDE like IntelliJ, it defaults to the project root. A relative path like 'data/file.txt' means different things in different environments.
How do I load a file that is inside my JAR?
Use getClass().getResourceAsStream('/path/inside/jar/file.txt'). Files inside the JAR cannot be accessed with new File() — they are not regular files on disk. getResourceAsStream() returns an InputStream that reads directly from the JAR, no matter where the JAR is located.
FileNotFoundException says '(Permission denied)' — what does that mean?
It means the file exists at that path, but the program does not have permission to open it. On Linux/macOS, check file permissions with ls -la and give read access with chmod. On Windows, right-click the file, go to Properties > Security and check that your user account has read permission.