• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help! Accessing files in a JAR file at Runtime

 
Chris Merritt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Merritt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic