PDA

View Full Version : Accessing resources in a java jar file


maitreya
March 5th, 2006, 09:43 AM
RandomAccessFile inFile;
inFile = new RandomAccessFile(textfile, "r");

Packaging the application into a jar file with the textfile. However running the jar file gives the error of "file not found". It seems that it tries to find the file outside the jar package. Any idea on how to fix this.

LordHunter317
March 5th, 2006, 12:58 PM
You have to open the JAR resource stream:InputStream stream = MyClass.class.getResourceAsStream(textfile);Have a party.

maitreya
March 5th, 2006, 01:54 PM
Thank you for your help. I managed to get it working:

BufferedReader inFile;
InputStream stream;
stream = MyClass.class.getResourceAsStream(textfile);
inFile = new BufferedReader(new InputStreamReader(stream));

wrapping it into the BufferedReader.