I need to be able to access a file stored in a compiled jar file. I have figured out how to add the file to the project, but how would I reference it in the code? How might I copy a file from the jar file to a location on the user's hard drive? I know there are dozens of ways to access a file (FileInputStream, FileReader, ect.), but I don't know how to look inside itself.
5 Answers
You could use something like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileFromJarFile);
If foo.txt was in the root of your JAR file, you'd use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("foo.txt");
assumes the class is in the same JAR file as the resource, I believe.

- 8,812
- 2
- 28
- 29
-
Part of what I need is BufferedReader which needs a FileReader as an imput. How can I convert an InputStream to a FileReader? – LRFLEW Mar 02 '11 at 20:53
-
You just play connect the dots with the Java documentation. Look at BufferedReader - In the See Also section, there's FileReader and InputStreamReader. Go to InputStreamReader, etc. – typo.pl Mar 02 '11 at 21:02
-
Yes, I can do that, but I just looked up BufferedReader, and found the parter class BufferInputStream which is what I need to use :P. – LRFLEW Mar 02 '11 at 21:43
-
Interesting: ... getResourceAsStream("foo.txt"); didn't work for me, but instead getResourceAsStream("/foo.txt"); – ShadowGames Dec 17 '21 at 10:16
You can use getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead.
For example:
BufferedReader reader = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("foo.txt")));

- 8,734
- 1
- 29
- 48
You could read the contents of a JAR file using the JarFile class.
Here's an example of how you could get a specific file from a JAR file and extract it:
JarFile jar = new JarFile("foo.jar");
String file = "file.txt";
JarEntry entry = jar.getEntry(file);
InputStream input = jar.getInputStream(entry);
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[input.available()];
for (int i = 0; i != -1; i = input.read(buffer)) {
output.write(buffer, 0, i);
}
} finally {
jar.close();
input.close();
output.close();
}

- 7,144
- 12
- 43
- 57
Just wanted to add that if we want to access file inside Jar that is located at the following path(only examples as resources loading is OS independent):
Windows:
c:\your-jar-file.jar\dir1\dir2\dir3\foo.txt
Linux:
/home/your-jar-file.jar/dir1/dir2/dir3/foo.txt
Will need to use following code(pay attention that there is NO "/"(forward-slash) character in the beginning of the path):
InputStream is = this.getClass().getClassLoader().getResourceAsStream("dir1/dir2/dir3/foo.txt");

- 654
- 8
- 10
-
How does getResourceAsStream know to look inside c:\your-jar-file.jar ? What if the jar file was completely separate from the jar file that the program is running from? – Michael Sims Feb 21 '22 at 18:25
-
I just gave examples of real OS dependant paths here to understand how a resource path syntax correlate with OS absolute path. You are right that java.lang.ClassLoader.getResourceAsStream only can load resources from jars that are managed by JVM you are running. I like to think of paths in their absolute form so that you can understand the most complex case it can be parsed. All this jars resources loading is handy if you want to ship jars that contain resources in them and be OS independent. – Constantin Zagorsky Mar 03 '22 at 09:43
-
If you want to access jar file that is completely separate from JVM loaded classpath you still need to load that jar into JVM process that needs it and simplest way is to define it on the classpath. It can be loaded programmatically(https://stackoverflow.com/a/5172103/4592525) which is a different subject and is not needed in most use cases as Java does a good job for you already with its default class loader. – Constantin Zagorsky Mar 03 '22 at 09:51
Look at the JarFile class. Everything you need to get the InputStream of a specific entry in the jar file is there.

- 9,352
- 6
- 34
- 49