'update Spring BOOT-INF/lib jars

I am trying to update a jar in rest-xxxx.jar, this rest jar is basically a SpringBoot Jar and has the folder structure of BOOT-INF/lib/<dependencies.jars> when you unpack it.

Now I want to replace a dependency jar within this rest-xxxx.jar, so I used the command:

jar uf rest-xxxx.jar BOOT-INF/lib/new-version-jar-same-name.jar

How ever I got errors during booting process:

Exception in thread "main" java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/new-version-jar-same-name.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file

I noticed that there's a 0 option for jar command to avoid compression, so I did:

jar u0f rest-xxxx.jar BOOT-INF/lib/new-version-jar-same-name.jar

But then I got a similar compression issue like below:

Exception in thread "main" java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/new-version-jar-same-name.jar/META-INF/MANIFEST.MF'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file

What should I do when I need to replace the dependency jars within a given springboot jar?

please note: I understand the best practise is to build from updated pom.xml, the limitations we have is that we cannot get the right version source code at the moment, hence we are trying to patch the existing springboot jar instead of rebuilding it.



Solution 1:[1]

Use the jar command to decompress the jar package, replace the jar package outside the compressed package, and compress it.

https://www.programmersought.com/article/48535890080/

Solution 2:[2]

My initial answer was deleted, so I'm following recommendations and updating my answer.

Regarding comments, sometimes it's faster to cut corners and just patch jar/zip file directly instead of rebuilding the whole thing that can take a lot of time on large projects.

There is nothing wrong with the Spring Boot file layout unless jar names and compression methods are retained, the nested .jars are only "indexed" by their names, and OP means replacing .jar with the same name.

I've faced the very same question as OP and haven't found a solution, so this is my attempt to solve it with utility https://github.com/denizk0/cpzip

This utility 1. Updates a file in the zip/jar, if it's a nested archive, it's expanded to a temp file, then GOTO 1. Nesting may be of any depth. Eventually, all nested archives are wrapped back together, temp files are cleaned. Compression methods are preserved.

Here's an example:

cpzip ~/.m2/repository/joda-time/joda-time/2.9.9/joda-time-2.9.9.jar camunda-bpm-run-7.16.0.zip internal/camunda-bpm-run-core.jar/BOOT-INF/lib -v

will copy or replace joda-time-2.9.9.jar from local maven repo in camunda-bpm-run-7.16.0.zip from current directory in nested internal/camunda-bpm-run-core.jar

PS: It is even possible to replace .js files in webjars

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shirel
Solution 2