This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Help!  Accessing files in a JAR file at Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Help!  Accessing files in a JAR file at Runtime" Watch "Help!  Accessing files in a JAR file at Runtime" New topic
Author

Help! Accessing files in a JAR file at Runtime

Chris Merritt
Greenhorn

Joined: Dec 02, 2009
Posts: 3
Using Netbeans... When I run, everything is solid. When I clean and build the JAR and run, I'm having problems accessing files in the Jar file.



Just running, the result is TRUE.
Building JAR and running, the result is FALSE.

run command is: java -jar dist/myjar.jar

HELP!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20050
    
  30

Just don't treat resources as files, because when inside a JAR, they aren't files - they are JAR entries (see JarFile and JarEntry). If you only need to read from the "file" use getResourceAsStream instead of trying to use File and FileInputStream. If you need to write to it you're simply out of luck.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Chris Merritt
Greenhorn

Joined: Dec 02, 2009
Posts: 3
I need to read, write, and hash file(s) that are in the JAR. So I'm outa luck I guess? I guess the alternative is a deployment/installation script that doesn't utilize a single JAR. Thanks for the info.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

You can't write into a jar file, no. Often what people do is to provide a template, or default, file in the jar. If there are changes required to that, then they write it to a real file and use that from then on. Whether that fits into your requirements I can't tell.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! Accessing files in a JAR file at Runtime
 
It's not a secret anymore!