View Full Version : Java problem (reading text from jar file)
View Full Version : Java problem (reading text from jar file)
fernandez
August 5th, 2003, 03:25 PM
My program is supposed to read a text file contained withing the jar file which encapsulates all classes, images and text files. The problem occurs when the program tries to read the text file inside the jar file.
I have a BufferedReader that's supposed to get the contents of the text, but I keep getting an error (java.io.FileNotFoundException). How do I specify the path so that the program will fetch the text file from the jar file?
Thanks,
fernandez
fernandez
August 5th, 2003, 04:19 PM
Nevermind, it's working now. You have to use an InputStream and call the getResourceAsStream method. I was using a regular FileReader to try to access the file.
juddman
June 20th, 2007, 05:41 AM
fernandez answers his own question, but I clarify both the question and the answer here.
The question is: how do you access a file that is inside the jarfile you have deployed your app or applet in? Using a FileReader will start searching the local directory where the app was launched, and will never look inside the jar file to find anything.
The answer is: Use the Class class, and call the getResourceAsStream() method. This has all the paraphernalia of the class loading system (which is already active in launching your app) available to it. See http://mindprod.com/jgloss/getresourceasstream.html
And if you get muddled, try starting your resource filename with a /.