[JAVA] How to change the contents of the jar file without decompressing


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.

  ┣ classes
    ┗ application.properties <- *I want to rewrite this file*
  ┗ lib
    ┗ {The library jar you are using}
  ┣ maven/
    ┗ {pom.xml}
  ┗ MANIFEST.MF <-Manifest file
┗ org/
  ┗ {Full class file}

Specifically, I tried to rewrite /BOOT-INF/classes/application.properties and it failed.

Failure example

Unzip and then zip

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.

Unzip and recreate with jar command

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

Success story

Replace the contents with the zip command without decompressing

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.

By the way

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

How to change the contents of the jar file without decompressing
[chown] How to change the owner of a file or directory
JAVA: jar, aar, view the contents of the file
Customize how to divide the contents of Recyclerview
[Rails] How to change the column name of the table
[Rails] How to get the contents of strong parameters
How to debug the generated jar file in Eclipse
How to change the setting value of Springboot Hikari CP
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
How to change the file name with Xcode (Refactor Rename)
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to create a jar file or war file using the jar command
[Rails] How to change the page title of the browser for each page
How to check for the contents of a java fixed-length string
How to get the length of an audio file in java
[Java] How to use the File class
How to delete the wrong migration file
How to delete the migration file NO FILE
How to add jar file in ScalaIDE
How to determine the number of parallels
How to change the timezone on Ubuntu
Ransack sort_link How to change the color!
How to sort the List of SelectItem
How to change the maximum and maximum number of POST data in Spark
How to find out the Java version of a compiled class file
How to change the value of a variable at a breakpoint in intelliJ
[Ruby On Rails] How to search the contents of params using include?
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to find the cause of the Ruby error
I want to var_dump the contents of the intent
How to get today's day of the week
Change the timezone of the https-portal container to JST
Rename the package name of the existing jar file
Output of how to use the slice method
Double-click to open the jar file on Windows
How Microservices Change the Way of Developing Applications
How to display the result of form input
[Java] How to get the authority of the folder
[Spring Boot] How to refer to the property file
How to set environment variables in the properties file of Spring boot application
I want to see the contents of Request without saying four or five
[Java] How to get the URL of the transition source
How to change the action with multiple submit buttons
How to delete / update the list field of OneToMany
[Java] How to extract the file name from the path
[Ruby] How to find the sum of each digit
[Ruby on Rails] How to change the column name
Test the contents of an Excel file with JUnit
How to install the root certificate of Centos7 (Cybertrust)
[Java] How to get the maximum value of HashMap
[SwiftUI] How to specify the abbreviated position of Text
JavaFX-Specify the location of the CSS file relative to the fxml file
[Java] Get the file in the jar regardless of the environment
How to judge the click of any area of the image
Java: Use Stream to sort the contents of the collection
How to download the old version of Apache Tomcat
[Swift] How to get the document ID of Firebase
I want to be aware of the contents of variables!
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
How to use git with the power of jgit in an environment without git commands