Bobby C
Bobby C

Reputation: 2085

How do I update one file in a jar without repackaging the whole jar?

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.

Upvotes: 32

Views: 92944

Answers (6)

Chasel Wu
Chasel Wu

Reputation: 71

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 *

Upvotes: 7

tbr
tbr

Reputation: 11

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

Upvotes: 0

Jose Alban
Jose Alban

Reputation: 7926

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

Upvotes: 0

Alireza Alallah
Alireza Alallah

Reputation: 2534

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

Upvotes: 6

Ankit Singh
Ankit Singh

Reputation: 2622

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.

Upvotes: 15

ziesemer
ziesemer

Reputation: 28687

-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

Upvotes: 46

Related Questions