Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?
Solution 1
zip -d file.jar unwanted_file.txt
jar is just a zip file after all. Definitely much faster than uncompressing/recompressing.
Solution 2
In case you want to delete file in order to unsign signed jar, you can probably just make the .RSA file zero-sized. This can be accomplished with just jar u
. See https://stackoverflow.com/a/24678645/653539 . (Worked for me, though I admit it's hack.)
Solution 3
In Java you can copy all the entries of a jar except the one you want to delete. i.e. you have to make a copy but don't need to create the individual files.
You can do this by
- creating a new jar.
- iterating though the Jar you have
- copy the entry from one jar to the other, skipping any files you want.
- close and replace the orginal jar if you want.
kellyfj
Making the rounds: Software Engineer -> Architect -> Manager -> Software Engineer --> ???
Updated on December 16, 2021Comments
-
kellyfj over 2 years
So I need to remove a file from a jar / war file. I was hoping there was something like "jar -d myjar.jar file_I_donot_need.txt"
But right now the only way I can see of doing this from my Linux command line (without using WinRAR/Winzip or linux equivalent) is to
- Do "jar -xvf" and extract the
complete Jar file - Remove the file(s) I don't need
- Rejar the jar file using "jar -cvf"
Please tell me there is a shorter way?
- Do "jar -xvf" and extract the
-
lapo almost 11 yearsI more often have p7zip installed instead of
zip
and in this case it's important to specify file format:7z d -tzip file.jar dir/unwanted_file.txt
-
Goaler444 over 10 years@martona I am getting the following error: zip error: Zip file structure
-
yby over 10 years@Goaler444 I had the same error, but using 7z as lapo suggested worked for me.
-
Tom over 9 yearsHow to delete a file from a child jar or grand child jar?
-
Martin Woolstenhulme over 7 yearsIf you want to delete a folder in the jar, make sure there is a trailing slash on the unwanted folder:
zip -d file.jar unwanted_folder/
-
vmishra over 6 yearsUsing 7z as @lapo suggested getting this error System error: E_NOTIMPL
-
Alex almost 4 yearsOne more point which cost me quite some time: you can't delete a non-empty folder. One must first delete all files/folders before deleting their parent.
-
Gena Batalski over 2 yearsworks also for *.txt and recursive (so be careful)