32

I have a .jar file called myfile.jar. Inside this jar file is a folder called images. Inside this folder called images, I have an image called hi.png. I want to update that image with a new version of that image, which is also called hi.png. I do not want to extract all of the files from the single jar file and then repackage them, I just want to update the image. So, I go to command line as usual, type a few lines of code, and then I do this command:

jar uf myfile.jar -C images hi.png

What I hoped to do with that command was to replace the old hi.png with the new hi.png. But, it gives me this error:

images\hi.png : no such file or directory

What do I do to fix this?

Extra info: I can not use something like WinRAR, I have to do this with command line.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Bobby C
  • 1,965
  • 3
  • 14
  • 10
  • 1
    **jar** files are also **zip** files, so the feature packed [zip tool](http://linux.about.com/od/commands/l/blcmdl1_zip.htm) can operate on them. If you're on anything non-Windows, you should have it already installed. (I think MacOS would have it, but I can't verify.) – Brigand Dec 11 '11 at 01:54
  • Not sure what your design is here... but 7Zip has a great command line support. Just be careful, the jar specification is picky about the exact type of zip. Secondly, you could probably use an expanded jar if this is web based. And... if it isn't web based (single client) why not just load the image from ./images/hi.png. TL;DR; Do it the easy way, if you're swapping on a filesystem use the filesystem. – Daniel B. Chapman Dec 11 '11 at 01:54
  • What directory are you doing this in (and where are images and hi.png relative to your cwd)? – James McLeod Dec 11 '11 at 01:55
  • 1
    @FakeRainBrigand its on all versions of OS X – Jon Egeland Dec 11 '11 at 01:58
  • 1) I'd generally use Ant or another build tool to build the Jars lazily (only when resources are updated) 2) Since Zip brings little benefit to most media types (at least sound, video & images), I'd also generally put them in a separate Jar with no compression. – Andrew Thompson Dec 11 '11 at 01:59

6 Answers6

42

-C is changing the local directory, and is looking for hi.png there. It's not controlling where you're trying to inject it into the JAR file.

I would try making a new directory called images, moving your local hi.png into that, making images a child directory of your current working directory, then just run this:

jar uf myfile.jar images\hi.png
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • 1
    It still gives me that error images\hi.png : no such file or directory – Bobby C Dec 11 '11 at 02:03
  • I just actually tested this, and it is working as expected. Make sure your working directory is the directory containing "images". Can you show an export of your command prompt showing the directory structure and the exact commands you're attempting? – ziesemer Dec 11 '11 at 02:07
  • 1
    +1 for joev's comment. Since I saw the "images\hi.png" in the error, I guess I assumed Windows. – ziesemer Dec 11 '11 at 02:08
  • Sorry, I completely did something different from what you said, everything is working fine now, Thanks so much! – Bobby C Dec 11 '11 at 02:29
  • 1
    I know it's an old question-answer, but I find it not working for spring boot runnable jar replacing "BOOT-INF/classes/logback-spring.xml" file, it gives me "Error: Invalid or corrupt jarfile" – linuxatico May 13 '19 at 17:09
  • the most important move is making sure you have the same directory structure on the working directory! – NanoNova Apr 06 '20 at 18:40
  • This is quite strange! The jar tool ought to be more intuitive. – gkns May 07 '20 at 05:45
  • This works for me for simple .war files manually generated but, when using a war file created by Spring, this command line replaces the whole original contents by the added hi.png file – coterobarros Mar 08 '23 at 15:37
14

The simplest way to do is using 7-zip software. For

  1. Editing a file:

    • Open the jar file 7-zip | open archive
    • goto the file e.g. /Meta-Inf/xyz.conf
    • right mouse click and select 'open inside' option
    • edit the file and save the file
    • close the 7-zip console and it's done.
  2. For adding/replacing/removing a file.

    • Follow the first two steps till you reach the desired folder.
    • Removing: delete the file
    • Adding: Drag and drop the file to the 7-zip console.
    • close the console and it's done.
Ankit Singh
  • 2,602
  • 6
  • 32
  • 44
6

you can use jar -uf sample.jar [path in jar file]target-file

Alireza Alallah
  • 2,486
  • 29
  • 35
5

You can use "jar" command line to extract your file and create a new jar file.

More detail you can check out from how to replace a image in a jar file

# create a new folder
mkdir contents
# extract jar file
jar xf example-1.5.jar
# Then you'll get a list of new files from jar
ls
# update your images in it
# create a new jar
jar cf example-1.5.jar *
Chasel Wu
  • 51
  • 1
  • 2
0

If you want to extend Spring Boot App (e.g. for e2e testing purposes), this one worked for me:

part of Dockerfile:

COPY ./libs/*.jar /BOOT-INF/lib/
RUN jar u0f /app.jar /BOOT-INF/lib/*.jar
tbr
  • 11
  • 2
-1

Jar files are similar to zip files. When you edit a zip file, vim unzips it and allows you to edit files inside of the zip file. Simply navigate over the file and press enter, and you will be able to edit files inside of the archive.

(Assuming you have unzip available, e.g. yum install -y unzip on CentOS)

vim my.jar

Jose Alban
  • 7,286
  • 2
  • 34
  • 19