Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

Code that calls new ClassPathResource("templates/report.txt").getFile() works in the IDE but throws FileNotFoundException when the app runs from the Boot jar. Why, and how do you fix it?

Inside the Boot jar, templates/report.txt is an entry in a zip archive, not a file on the file system, so ClassPathResource.getFile() cannot return a java.io.File and throws FileNotFoundException. In the IDE it worked because the classpath there is a directory such as target/classes, where the resource really is a file. The fix is to read it through getInputStream(), which works for files and jar entries alike, and to obtain the resource by injecting it with @Value("classpath:templates/report.txt") Resource or through a ResourceLoader.

Why this question is asked

This is one of the first production surprises many Spring Boot developers meet: everything works on the laptop, the jar is built and deployed, and the first report fails with FileNotFoundException for a file that is clearly inside the jar. Interviewers like it because it tests whether we understand the difference between a classpath resource and a file, and because the fix is simple once the cause is clear. In this answer, we will reproduce the problem, see why the IDE and the jar behave differently, read the resource the right way, inject it cleanly, and handle the rare case where a library really needs a File.

1Reproducing the Problem

Here is the code from the question, with the result in each environment.

Java
Resource resource = new ClassPathResource("templates/report.txt");
File file = resource.getFile();
String template = Files.readString(file.toPath());
// In the IDE: works, file = /work/app/target/classes/templates/report.txt
// java -jar app.jar: java.io.FileNotFoundException: class path resource [templates/report.txt]
// cannot be resolved to absolute file path because it does not reside in the file system:
// jar:nested:/opt/app/app.jar/!BOOT-INF/classes/!/templates/report.txt

The message is the clue: the resource does not reside in the file system. Its URL starts with jar:, meaning it lives inside an archive; the exact URL form depends on the Boot version, older versions show jar:file:...!/BOOT-INF/classes!/... instead.

2Why the IDE and the Jar Behave Differently

When we run the application from the IDE or with mvn spring-boot:run, the classpath contains the directory target/classes (or build/resources/main with Gradle). Resources were copied there as ordinary files, so the resource's URL is file:/work/app/target/classes/templates/report.txt, and getFile() can turn that into a java.io.File.

When we run java -jar app.jar, the classpath is inside the jar. Spring Boot's executable jar keeps our classes and resources under BOOT-INF/classes inside the archive, and a jar is a zip file. An entry in a zip file has no path on the disk, so there is nothing a java.io.File could point to. getFile() can only work for resources that are real files, and it throws for everything else.

The same resource in two environments
Where it runsResource URLgetFile()getInputStream()
IDE or spring-boot:runfile:/.../target/classes/templates/report.txtworksworks
java -jar app.jarjar:...app.jar!/BOOT-INF/classes!/templates/...FileNotFoundExceptionworks

3The Fix: Read Through getInputStream()

A Resource can always be read as a stream, whether it is a file, a jar entry, a URL or a byte array. So we read the contents instead of asking for a File. Since Spring Framework 6.0, Resource also has getContentAsString(Charset), which reads the stream for us.

Java
Resource resource = new ClassPathResource("templates/report.txt");
try (InputStream in = resource.getInputStream()) {
String template = new String(in.readAllBytes(), StandardCharsets.UTF_8); // works in IDE and jar
}
// Or, on Spring Framework 6.0 and later:
String template = resource.getContentAsString(StandardCharsets.UTF_8); // works in IDE and jar

Notice that we name the charset. Relying on the platform default makes the result depend on the machine, while the template file is almost always UTF-8.

4Injecting the Resource Instead of Creating It

Creating a ClassPathResource by hand works, but injecting a Resource is cleaner and easier to test. Spring converts a location string into a Resource for us, using the prefix to decide where to look, so the same code can later read from the file system just by changing configuration.

Java
@Service
class ReportService {
private final String template;
ReportService(@Value("${reports.template:classpath:templates/report.txt}") Resource template) throws IOException {
this.template = template.getContentAsString(StandardCharsets.UTF_8); // read once at startup
}
}
// Or with a ResourceLoader when the name is known only at runtime:
Resource r = resourceLoader.getResource("classpath:templates/" + name + ".txt");

Reading the template once in the constructor has a second benefit: if the file is missing, the application fails at startup with a clear error rather than at the first report request.

5When a Library Really Needs a File

Occasionally a third-party API accepts only a java.io.File or a path, for example an old reporting engine. In that case we copy the resource to a temporary file once and pass that file on. It is a workaround, so we keep it at the edge and delete the file when we no longer need it.

Java
Path tmp = Files.createTempFile("report", ".txt");
try (InputStream in = resource.getInputStream()) {
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
}
legacyEngine.load(tmp.toFile()); // a real file on disk, in the IDE and in the jar

Before using this, we check whether the library also accepts an InputStream or a URL; most do.

6Follow-up questions

Why does resource.exists() return true in the jar when getFile() fails?

Because exists() checks whether the resource can be found and opened, which is true for a jar entry. Being present and being a file on disk are different questions, and only getFile() asks the second one. isFile() is the method that tells us whether getFile() can work.

Would the same problem happen with Paths.get(url.toURI())?

Yes, for the same reason. A jar: URI cannot be turned into a default file system path. Code that needs a Path inside a jar must open the jar with a zip FileSystem, which is rarely worth it compared with reading the stream.

How do you catch this before production?

Run at least one test or smoke check against the packaged jar, for example by starting java -jar in CI and calling a health or report endpoint. A rule in code review, or a static analysis check that flags Resource.getFile() for classpath resources, also helps.

7Conclusion

In this answer, we saw that getFile() works in the IDE because the classpath is a directory of real files, and fails in the Boot jar because the resource is an entry inside an archive with no path on disk. The fix is to read the resource with getInputStream() or getContentAsString(), to obtain it by injecting a Resource with @Value or through a ResourceLoader, and, only when a library insists on a File, to copy it to a temporary file first.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is that inside a Spring Boot jar the resource is not a file. When I run from the IDE, the classpath is a directory like target/classes, so templates/report.txt really exists on disk and getFile can return a java.io.File.

Then, if they want more

When I run java -jar, our classes and resources are entries under BOOT-INF/classes inside the jar, which is a zip archive. An entry in a zip has no path on the file system, so getFile throws FileNotFoundException, and the message says the resource does not reside in the file system and shows a jar URL.

The fix is to stop asking for a File and read the content instead. getInputStream works for files, jar entries and URLs alike, and since Spring 6 there is also getContentAsString with an explicit charset, which I would set to UTF-8.

I would also not create the ClassPathResource by hand: I would inject it with @Value of classpath:templates/report.txt as a Resource, or use a ResourceLoader if the name is only known at runtime, and read it once at startup so a missing file fails fast.

If some legacy library really insists on a File, I copy the stream to a temporary file and pass that. And to catch this class of bug earlier, I like one CI check that starts the real jar.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Code that calls new ClassPathResource("templates/report.txt").getFile() works in the IDE...": https://scrivio-interview-library.netlify.app/spring-boot/spring-utilities/cb6ca29301/