In my Java project work, I built the source code as a jar and deployed it to the server. When I wanted to rewrite the contents of the deployed jar file for testing, I got a little stuck, so I will write down the solution here.
In this article, we will treat the Spring framework project as an example. The directory when decompressing the jar file is as follows.
┣ BOOT-INF/
┣ classes
┗ application.properties <- *I want to rewrite this file*
┗ lib
┗ {The library jar you are using}
┣ META-INF/
┣ maven/
┗ {pom.xml}
┗ MANIFEST.MF <-Manifest file
┗ org/
┗ {Full class file}
Specifically, I tried to rewrite /BOOT-INF/classes/application.properties
and it failed.
The jar file is practically the same as the zip file, so I unzipped the jar file with the zip
command, modified the file, and tried to zip it again with the zip
command.
When I tried to start the app with the java -jar
command based on the resulting file, I got the following error.
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/bcpkix-jdk15on-1.60.jar
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/bcpkix-jdk15on-1.60.jar'
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:256)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:241)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/bcpkix-jdk15on-1.60.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
at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:284)
at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:264)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
... 6 more
As the error message says nested jar files must be stored without compression
, the jar files in the libraries under BOOT-INF / lib
have been compressed (?) And they were not read as jar files. It seems.
If the simple zip command doesn't work, try creating a jar file with the jar command.
jar cvfm xxx.jar ./META-INF/MANIFEST.MF ./
When I tried to start it based on the jar file created by the above command, I got the following error.
no main manifest attribute, in xxx.jar
I have specified the manifest file as an option, but it doesn't seem to load properly (the direct cause of this phenomenon is unresolved).
By rewriting the contents without unzipping the jar file, the above problem was cleared.
First, reproduce the file you want to rewrite and the directory where it is located in your local location.
I wanted to rewrite the file BOOT-INF / classes / application.properties
this time, so I will create it.
mkdir -p BOOT-INF/classes && touch ./BOOT-INF/classes/application.properties
After rewriting this local ʻapplication.properties file, zip it. Bring the zip file (
xxx.jar`) you want to import to the same directory and import it with the following command.
zip ./xxx.jar ./BOOT-INF/classes/MANIFEST.MF
The resulting jar file did not cause an error, probably because it had never been unzipped, and it worked fine.
The above is how to rewrite the contents of the jar file. If you just want to see it instead of rewriting the contents, you can see it with the following command.
jar tf xxx.jar
My boss got angry when I was unzipping the jar file to check the contents.
Recommended Posts