Updating properties file inside JAR

I am trying to write a small app. that needs to save some input data and use it the next time it is being executed.
Currently, the application is executed by using an executable JAR and I don't wish to change it. The thing is that I also don't wish to write any external resource files near that jar.
My question is how can I write, or actually update, a properties file within the jar file without destroying the JAR content(and doing it at runtime).
Code snippet will be the best...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AFAIK, that's not possible, but take some other opinions too.
Mick BarryJava DeveloperCommented:
you can't, options are:
- recreate jar, including properties changes
- use properties file external to jar in a known location such as users home directory

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shji1Author Commented:
Can I load the content to to the memory and then write it back to the same JAR with the modifications?
Wont it throw exceptions for the application already running?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

>>Can I load the content to to the memory and then write it back to the same JAR with the modifications?

shji1Author Commented:
Mick BarryJava DeveloperCommented:
> Can I load the content to to the memory and then write it back to the same JAR with the
> modifications?

that was the first alternative I suggested trying.
Easiest would be to store the properties file in it's own jar.

Mick BarryJava DeveloperCommented:
What exactly is you want to achieve, and why don't you want to store it externally?
shji1Author Commented:
I guess CEHJ already tried it before...

> Easiest would be to store the properties file in it's own jar.
Easier to just save it outside as is.
shji1Author Commented:
I don't want it to be so easily modified by any curious person.
>>I guess CEHJ already tried it before.

No - i haven't - that's why i expressed doubt originally. I'm quite willing to be convinced otherwise though by some concrete evidence ;-)
Mick BarryJava DeveloperCommented:
> I don't want it to be so easily modified by any curious person.

No difference if its in it own jar, or if its in the same jar as app.
Other option is to simply encode it somehow.
shji1Author Commented:
I guess it's a matter of preference.
Mick BarryJava DeveloperCommented:
Storing it externally, actually makes it a little harder cause the user doesn't know where the properties file is.
shji1Author Commented:
This is true if you are the one installing it. It's not the case here.
It's a very small app that I wish to mail to some people.

Well... At least you saved me time (-;
 Remind me how can I split the points?
shji1Author Commented:
o... found it (-:
Clarification here - where are we? Write to own jar possible or not?
shji1Author Commented:
probably write it to external properties file.
had to compromise....
Ah - i was hoping you were going to keep this open until you'd established whether this is possible...
shji1Author Commented:
O... Sorry (-:
Actually I still think it's possible to load all the jar data into byte[] buffers and then to write it back to the same jar on runtime.
I just hoped that I wont have to do it and then realise that it can't be done. But now, I think I am going to try anyway.
I'll try to let you know somehow.
OK - keen to know. Please post in here later.
Mick BarryJava DeveloperCommented:
The only possible problem with writing back to the same jar would be that it is already open.
I'm 99% certain it's not possible
On MSWindows you can!
Download TotalCommander from http://www.ghisler.com/
then associate the properties file with an editor (e.g. notepath)
in TotalCommander, open the .jar as if it were a .zip file (Ctrl/PgDn),
click on the file to edit and your editor pops up with the file.
Afterwards save and close.
TotalCommander asks you if the .jar must be refreshed; say Yes.

The utility mentioned is Freeware, so you don't have to buy it yet.
I'm more than a little mystified by that last comment ;-) shji1 is talking about a programmatic write to a running program's own jar...
You are right; sorry.
Do you know an icon for a blush ......?

There may be one in the Unicode set..;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.