Quick revision
The short answer
new File("src/main/resources/...") works only because the IDE runs from the project folder; in a packaged jar the template is an entry inside the jar, not a file on disk, and src/main/resources does not exist at all. getClass().getResource("templates/mail.html") fails because a relative name is resolved against the class's own package, so it looks for com/acme/mail/templates/mail.html; we need a leading slash, getResource("/templates/mail.html"). The correct approach is to read the resource as a stream with getResourceAsStream in try-with-resources, or in Spring to use ClassPathResource or an injected classpath: Resource.
What to remember
In this answer, we saw that new File("src/main/resources/...") works in the IDE only because the working directory is the project folder, while in a jar the resource is an archive entry and the source tree does not exist. Class.getResource resolves names without a leading slash relative to the class's package, so we need /templates/mail.html, while ClassLoader.getResource takes root-relative names without a slash. The correct approach reads resources as streams with getResourceAsStream and try-with-resources, and in Spring uses ClassPathResource or an injected classpath: Resource.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
new File("src/main/resources/...") works only because the IDE runs from the project folder; in a packaged jar the template is an entry inside the jar, not a file on disk, and src/main/resources does not exist at all. getClass().getResource("templates/mail.html") fails because a relative name is resolved against the class's own package, so it looks for com/acme/mail/templates/mail.html; we need a leading slash, getResource("/templates/mail.html"). The correct approach is to read the resource as a stream with getResourceAsStream in try-with-resources, or in Spring to use ClassPathResource or an injected classpath: Resource.
Why this question is asked
Code that works in the IDE and fails in production is always worth understanding, because the difference usually reveals an assumption we did not know we were making. With resources, the assumption is that our files are files. Interviewers ask this because two separate mistakes hide in two short lines, and because the correct patterns are simple once we know why. In this answer, we will recap what happens to resources at build time, explain each of the two problems, see the difference between Class.getResource and ClassLoader.getResource, and then write the correct code in plain Java and in Spring.
1A Quick Recap: What Happens to Resources at Build Time
In a Maven or Gradle project, src/main/resources is a source folder. At build time, its contents are copied into the build output, target/classes or build/resources/main, next to the compiled classes, so templates/mail.html ends up at the root of the class path as templates/mail.html. When the application is packaged, that output becomes the content of the jar: the file is now an entry named templates/mail.html inside app.jar, or inside BOOT-INF/classes in a Spring Boot jar.
A resource, then, is anything the class loader can find on the class path by name, using the same search it uses for classes. The name is a slash-separated path relative to the root of the class path, never a file system path.
2Problem 1: new File With a Source Path
The first line builds a path relative to the current working directory.
It works in the IDE only by coincidence: the IDE starts the program with the project folder as the working directory, and the source tree happens to be there. In production, the working directory is wherever the service was started, and the source tree is not deployed at all. Even if we pointed the File at the jar's contents, there is no file: a jar entry is compressed data inside a zip archive, and java.io.File can only describe paths in the file system.
3Problem 2: A Relative Name in Class.getResource
The second line uses the class path correctly, but with the wrong name. Class.getResource resolves a name without a leading slash relative to the package of the class we call it on.
| Call | Name given | Looks for |
|---|---|---|
MailService.class.getResource | templates/mail.html | com/acme/mail/templates/mail.html |
MailService.class.getResource | /templates/mail.html | templates/mail.html |
ClassLoader.getResource | templates/mail.html | templates/mail.html |
ClassLoader.getResource | /templates/mail.html | nothing: returns null |
So Class.getResource and ClassLoader.getResource use opposite conventions. Class.getResource treats a leading slash as the class path root and anything else as relative to the class's package, which is convenient for resources that sit next to a class. ClassLoader.getResource always works from the root and expects no leading slash; with one, it finds nothing. Notice also that the found URL has the jar: scheme, a URL pointing inside the archive, which cannot be turned into a File.
4The Correct Way in Plain Java
Since a resource inside a jar is not a file, we read it as a stream, which works the same whether the class path entry is a directory or a jar.
There are three details. We use MailService.class rather than getClass(), because getClass() returns the runtime class, which for a subclass or a Spring proxy may live in another package and change what a relative name means. We check for null, because getResourceAsStream returns null rather than throwing when nothing is found, and a clear message saves a NullPointerException later. And try-with-resources closes the stream, which matters because each open stream on a jar entry holds resources. If we need a real file, for example for a library that only accepts a path, we copy the stream to a temporary file first.
5The Correct Way in Spring
Spring's Resource abstraction handles the class path, files and URLs with one interface, and it resolves classpath: locations from the root.
The classpath: prefix always means the class path root, so there is no relative-name surprise. As with plain Java, we read through getInputStream or getContentAsString, and avoid getFile(), which only works when the resource happens to be a real file and throws FileNotFoundException inside a jar.
6Follow-up questions
Why does getResource sometimes work in tests but not in the jar?
Because in tests the class path entries are directories such as target/classes and target/test-classes, so a URL from getResource is a file: URL and code that converts it to a File or Path works. Inside a jar the URL is a jar: URL, and the same conversion fails. Test the packaged artifact, or avoid File conversions entirely and use streams.
Which class loader should a library use to find resources?
Usually the loader that loaded the library's own classes, through SomeLibraryClass.class.getResourceAsStream, for resources the library ships itself. For resources that belong to the application, the thread context class loader is often the right choice in containers, because it sees the application's classes. Documenting which one a library uses saves users a lot of confusion.
7Conclusion
In this answer, we saw that new File("src/main/resources/...") works in the IDE only because the working directory is the project folder, while in a jar the resource is an archive entry and the source tree does not exist. Class.getResource resolves names without a leading slash relative to the class's package, so we need /templates/mail.html, while ClassLoader.getResource takes root-relative names without a slash. The correct approach reads resources as streams with getResourceAsStream and try-with-resources, and in Spring uses ClassPathResource or an injected classpath: Resource.
Open with
There are two separate problems. The File one works in the IDE only because the IDE runs the program with the project folder as the working directory, so the relative path happens to point into the source tree.
Then, if they want more
In production the working directory is somewhere else, src main resources is not deployed at all, and the template is an entry inside the jar, which is not a file, so java.io.File cannot describe it anyway. The getResource one uses the class path correctly but with the wrong name.
Class.getResource with a name that has no leading slash is resolved relative to the class's own package, so from com.acme.mail it looks for com/acme/mail/templates/mail.html and returns null. With a leading slash, slash templates slash mail.html, it resolves from the class path root and finds it. ClassLoader.getResource is the opposite: names are always root-relative and must not start with a slash.
The correct way is to treat it as a resource stream: MailService.class.getResourceAsStream with the leading slash, check for null, and read it inside try-with-resources, for example readAllBytes into a UTF-8 string. In Spring I would inject @Value classpath:templates/mail.html as a Resource, or use ClassPathResource, and read it with getInputStream or getContentAsString, never getFile, because getFile fails inside a jar.
About this answer
- Last updated
- 12 November 2026
- 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 "This code works in the IDE but returns null when run from the packaged jar: new...": https://scrivio-interview-library.netlify.app/java/class-loading/1cefe3f6f0/