Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?


Java Problem Overview

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?

Java Solutions

Solution 1 - Java

zip -d file.jar unwanted_file.txt

jar is just a zip file after all. Definitely much faster than uncompressing/recompressing.

Solution 2 - Java

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.

Solution 3 - Java

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 . (Worked for me, though I admit it's hack.)

Solution 4 - Java

If you wish to do this programatically, can use the Zip File System to treat zip/jar files as a file system. This will allow you to edit, delete, and add files to the jar file.



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkellyfjView Question on Stackoverflow
Solution 1 - JavamartonaView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaTomáš ZáluskýView Answer on Stackoverflow
Solution 4 - JavaGamebuster19901View Answer on Stackoverflow