• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Modifying a source file from a JAR

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need a bit of help trying to figure out an issue I'm having with my program. If I run my program as is in my IDE (before exporting it to an executable JAR) I am able load in a txt file with some values, modify that file by making a temporary copy of it, and then renaming the modified file to the name of the original or source file. However, when I create my JAR, I am only able to load in the original file. There is no way, apparently, to do a write to the source file because the JAR essentially locks down the text file I need to modify.

Now, my question is how can I simulate this copying of the source file, modification, and then renaming of the modified file to the original? Am I thinking about this all wrong? Here is my code I'm using thus far (the deleteFromFile method is where I am stuck in terms of getting this to work with my JAR):

 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you've found, you can't update resources which are in a JAR file. So the way to go is to store your modifiable text file somewhere else.

So your question now is, where should you store it? You should store it in the user's home folder, or perhaps in a subfolder of that which is specific to your application. You can find the user's home folder like this:



As for the simulating you asked for, the algorithm is like this:

1. Did you already write a modified version in the user's home folder? If so, read that. If not, use the code you already have to read the resource from the JAR.

2. Write the newly modified version into the user's home folder.
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jars are supposed to contain files which are needed for your application to function properly, and are supposed to be readonly. Paul has suggested nice way of doing what you need. go for it.
 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thank you so much for the insight. I am going to try to implement the approach you suggested. Will let you know how everything works out!

Thanks again.
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clear question and relevant code, Mike. Have a cow for that!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic