Hello Stack Overflow friends. I have a simple problem which i fear doesnt have a simple solution and i need advice as to how to proceed. I am developing a java application packaged as and executable JAR but it requires to modify some of its JAR file contents during execution. At this stage i hit a problem because some OS lock the file preventing writes to it.
It is essential that the user sees an updated version of the jar file by the time the application exits allthough i can be pretty flexible as to how to achieve this. A clean and efficient solution is obviously prefereable but portability is the only hard requirement.
The following are three approaches i can see to solving the problem, feel free to comment on them or suggest others.
I know this is a weird question but any help would be appreciated.
One option:
Have the program write a modified copy of the JAR file.
Include in the JAR file a second JAR with a utility that, when executed, deletes the original JAR file and renames the modified copy to match that of the original.
When your program exits (and changes have been made) extract this utility and launch it in its own JVM (using Runtime.getRuntime().exec()
). The utility would wait for the lock to go off the original, unmodified JAR and then do its business and exit.
To the user the JAR file would seem to update on exit (or close enough!).
I honestly think your approach is wrong headed. This is simply not a good fit to the default way of deploying java. Your modified files will just have to persist to a db or xml file somewhere - that is the ONLY sensible approach.
Anything is just "fighting against the wind" - you might with some struggling get it to work, but it WILL bite you or customer eventually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With