Update a file inside a JAR in a specific directory with jar command
jar, java
Solution
4129445 : An API to incrementally update ZIP files in the Sun/Oracle bug database asks for this feature to be implemented in the java api.
From the Evaluation:
"It is easy to sympathize with those who want this bug fixed. It is perhaps easier to sympathize with those who have, inadvertently, overwritten JAR files that are already in-use by a running JVM."
and
"It would have been nice if the jar & zip APIs had allowed for mutable zip files from day one. But at this point adding mutability to jar & zip files from Java(or rather, increasing the ease of mutating them) seems likely to introduce more hard-to-debug problems."
Problem
So I've been looking high and low for an answer to this and obviously haven't found a satisfactory answer. The problem is that I want to update a JAR (or any file for that matter) inside a JAR, but said file is inside a folder structure, which I would like to avoid reproducing outside the JAR just so I can update it. Example: ``` Foo.jar (root directory) / |->/folder1 | |->/folder2 | |->/foo | |-->/bar | |---->/baz | |------>file_to_update.jar ``` So, if I want to update this using the jar command, I would have to first create the same folder structure outside the JAR and then do ``` jar -uf Foo.jar -C foo/bar/baz/file_to_update.jar ``` However I would like to avoid creating this folder structure since for certain files it can be quite deep and also because I might need to update selected files all over the container JAR, so I would not like to create so many folders with a specific structure just so the jar command can update them. To put it in another way, I would like to tell the jar command to update a specific destination file inside a specific destination directory, using a specific source file inside a specific source directory. I.e. something like ``` jar -uf Foo.jar -sourceFile /x/y/z/filetoupdate -destFile /a/b/c/filetoupdate ``` (I know this syntax doesn't work, it's just to exemplify what I need). Can this be done? It would strike me as very odd that I would absolutely need to mimic the whole folder structure when the jar command could find a match for it or something, since I could have the need to update a file inside a folder structure that is 100 folders deep. That would be overly complex to reproduce just to update the file. Also, I know a regular ZIP utility could be used, but the one I have available in Linux is not updating the file even if it says it did (upon re-opening the jar, it has the old version intact), and right now I can't go looking for another one (company policy, no time, testing issues, you name it). Finally, if I can do this from a command line, it means I can create a batch file to update lots of specific files without having to create the folder structure. I forgot to say that I would also like to avoid having to unjar, update, rejar the whole thing, as this JAR can go upwards to 2GB so it takes a while to do this just to, basically, add a little resource file to my gigantic JAR. Any help on this is greatly appreciated!