Reading a resource file from within jar
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Reading a resource from inside a JAR is not the same as opening a normal filesystem path. Once a file is packaged into the application's classpath, you should load it as a classpath resource through Class or ClassLoader, usually as an InputStream.
Think in Terms of Classpath Resources
A file inside a JAR is not a standalone file sitting on disk in the ordinary sense. It is part of the classpath. That means code such as this is usually the wrong model:
That may work in development when the source tree is visible, but it usually fails once the application is packaged and run from a JAR.
Use getResourceAsStream()
The normal answer is to load the resource as a stream:
With Class.getResourceAsStream(), the leading / means "start from the classpath root."
ClassLoader.getResourceAsStream() Is Slightly Different
You can also load through the class loader:
The path form is slightly different here:
- '
Class.getResourceAsStream("/config.txt")uses a leading slash for an absolute classpath lookup' - '
ClassLoader.getResourceAsStream("config.txt")does not use the leading slash'
Both are valid, but mixing their path rules is a common source of null results.
Reading Text Safely
If the resource is text, wrap the stream in a reader and choose the charset explicitly when appropriate:
This avoids platform-default charset surprises.
If You Need Bytes Instead of Text
Not every resource is text. For images, binary blobs, or templates you want to pass elsewhere, read the bytes directly:
The loading mechanism is the same even though the interpretation is different.
Do Not Assume a Real Filesystem Path Exists
Another common mistake is asking the resource for a File path and then treating it like a normal filesystem location. That can break when the application actually runs from a packaged JAR because the resource is inside the archive, not available as a regular external file.
If a library absolutely requires a real file path, the usual workaround is to copy the resource stream to a temporary file first.
Package Placement Matters
The resource must actually be included in the built JAR. In a standard Java or Maven project, resources typically live under a classpath-managed directory such as src/main/resources. If the build never packages the file, your loading code can be correct and still return null.
That is why resource-loading bugs are often half code issue and half build configuration issue.
Common Pitfalls
- Trying to read a JAR resource with
new File(...)instead of classpath APIs. - Mixing up
Class.getResourceAsStream()andClassLoader.getResourceAsStream()path rules. - Forgetting to check for
nullwhen the resource is missing. - Relying on the default character encoding for text resources.
- Assuming a resource inside a JAR has a normal filesystem path you can always pass to file-based APIs.
Summary
- Files inside a JAR should be treated as classpath resources, not normal filesystem files.
- Use
getResourceAsStream()orgetClassLoader().getResourceAsStream()to load them. - Be careful about path syntax differences between
ClassandClassLoader. - Read text with an explicit charset when possible.
- If a library needs a real file path, copy the resource stream to a temporary file first.
Related reading
- Read/Write String from/to a File in Android
- Read/write to Windows registry using Java
- reason no instances of type variables T exist so that void conforms to using mockito
- Reasons for using a Bag in Java
- Receiving Kafka Key in spring boot kafka listener
- Recommended way to get hostname in Java
- Recover an Asynch ThreadPoolTaskexecutor after server crashed/shut down
- Recursively list files in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.