File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Reading file from jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading file from jar file" Watch "Reading file from jar file" New topic

Reading file from jar file

Gail Mikels
Ranch Hand

Joined: May 07, 2001
Posts: 634
I am trying to read a file that I'm using as a template. The template file is in the deployed jar file for my application. I'm thinking my syntax for reading a file from a jar file is incorrect, though I've tried several examples I've found, and none seem to work.

Here's my code snippet:

And here's the relevent output & error:

I've searched online & it seems that I'm doing it right. But obviously I'm not! I've tried making the slashes go the other way, tried finding other files in that same directory (also unsuccessful), opened up my jar file to verify that my template file is at the path I'm looking at. To no avail.

Any ideas?? :-)

Gail Mikels
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Resources inside jar files aren't "files". Only things on disks that your operating system manages as files are "files". So you can't use a FileInputStream. Do this instead:
Gail Mikels
Ranch Hand

Joined: May 07, 2001
Posts: 634
Oh. <duh>

Thank you very much!!! :-D
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
thannks very much... the exact answer which i confused...

"Many of lifes failure are people who did not realize how close they were to success when they gave up."
-Thomas Edison
I agree. Here's the link:
subject: Reading file from jar file
jQuery in Action, 3rd edition