133

So I need to remove a file from a .jar or .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 the Linux equivalent) is to

  • Do jar -xvf and extract the complete .jar file
  • Remove the file(s) I don't need
  • Re-jar the .jar file using jar -cvf

Please tell me there is a shorter way.

2
  • 3
    Since a "jar" is just a zip file, I suspect you'll have better luck on superuser.com.
    – Kirk Woll
    Commented Dec 23, 2010 at 17:00
  • You can also try writing a shell script to wrap the whole process. Something like ./myscript.sh -d <list of files>
    – Favonius
    Commented Dec 23, 2010 at 17:05

5 Answers 5

263
+200
zip -d file.jar unwanted_file.txt

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

8
  • 5
    I 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
    – lapo
    Commented Jul 31, 2013 at 10:02
  • 2
    @martona I am getting the following error: zip error: Zip file structure
    – Goaler444
    Commented Dec 8, 2013 at 12:44
  • @Goaler444 I had the same error, but using 7z as lapo suggested worked for me.
    – yby
    Commented Jan 21, 2014 at 22:04
  • 7
    If 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/ Commented Dec 20, 2016 at 23:53
  • 1
    One 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.
    – Alex
    Commented Aug 5, 2020 at 15:17
3

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.)

3

The best answer for me was in a comment by lapo on another answer on this post.

User lapo wrote:

I 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

User lapo's comment is in response to an answer suggesting using zip -d directly since the .jar files are like .zip archives but not exactly the same format. Like others, I wasn't able to use zip -d file.jar unwanted_file.txt. That would lead to

zip error: Zip file structure invalid

But, I was able to install p7zip via brew install p7zip, and then I was able to delete using 7z d -tzip file.jar unwanted_file.txt.

1

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.
0

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.

See Appending files to a zip file with Java

Not the answer you're looking for? Browse other questions tagged or ask your own question.