Linux替換jar包中內容

因fastjson低版本存在漏洞,故線上服務需要升級到fastjson-1.2.70.jar,替換jar服務中的低版本jar包

1、創建一個臨時目錄
    mkdir temp
2、將jar添加到目錄
    cp ***.jar temp    
3、解壓
    jar -xvf ***.jar
    更新解壓後的文件
4、刪除臨時目錄中的jar包,重新壓縮打包
    rm -rf ***.jar
    jar -cfM0 ***.jar ./

-0 產生jar包時不對其中的內容進行壓縮處理
-M 不產生所有文件的清單文件(Manifest.mf)。這個參數與忽略掉-m參數的設置

    
在壓縮jar包時,使用如下命令打出的jar均不能正常啓動

tar -cvf ***.jar ./
啓動報錯:Error: Invalid or corrupt jarfile ***.jar

jar -cvf ***.jar ./
啓動報錯:no main manifest attribute, in ***.jar
原因:重新生成的問題清單文件MANIFEST.MF覆蓋了原文件,原本jar中lib目錄下的jar都被再次壓縮了

jar -cfM ***.jar ./
啓動報錯:

Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/slf4j-api-1.7.25.jar'
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:237)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:104)
        ... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/slf4j-api-1.7.25.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:285)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:260)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:248)
        ... 6 more


原因:原本jar中lib目錄下的所有jar都被再次壓縮了

手動替換jar中內容
啓動報錯:

Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/fastjson-1.2.70.jar'
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:237)
        at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:104)
        ... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/fastjson-1.2.70.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:285)
        at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:260)
        at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:248)
        ... 6 more


原因:fastjson-1.2.70.jar添加到jar中後被再次壓縮了   

 

lib中的jar正常情況應該是

被再次壓縮後

重新生成的問題清單文件Manifest.mf

正常情況應該有更多詳細信息

jar命令參數:

-c  創建一個jar包
-t 顯示jar中的內容列表
-x 解壓jar包
-u 添加文件到jar包中
-f 指定jar包的文件名
-v  生成詳細的報造,並輸出至標準設備
-m 指定manifest.mf文件.(manifest.mf文件中可以對jar包及其中的內容作一些一設置)
-0 產生jar包時不對其中的內容進行壓縮處理
-M 不產生所有文件的清單文件(Manifest.mf)。這個參數與忽略掉-m參數的設置
-i    爲指定的jar文件創建索引文件
-C 表示轉到相應的目錄下執行jar命令,相當於cd到那個目錄,然後不帶-C執行jar命令

 

參考:

https://www.jianshu.com/p/9efc96b3057a

https://www.cnblogs.com/zhjh256/p/10986920.html

https://www.cnblogs.com/jiftle/p/9068354.html 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章