(1). 需求
现在微服务,都是直接一个jar包(实际是一个zip),需要往jar里添加内容,今天通过一些工具,直接往jar里添加内容时报错.
原因:替换或者导入jar包时,jar包被自动压缩,springboot规定嵌套的jar包不能被压缩.
(2). Unable to open nested jar file
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/route-service-1.0.0-SNAPSHOT.jar'
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:288)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:274)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:100)
... 6 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/route-service-1.0.0-SNAPSHOT.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:314)
at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:296)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:285)
(3). 查看jar包内容
infinova@lixin Desktop % jar -tf gateway.jar | tail
BOOT-INF/lib/guice-4.1.0.jar
BOOT-INF/lib/javax.inject-1.jar
BOOT-INF/lib/aopalliance-1.0.jar
BOOT-INF/lib/snakeyaml-1.26.jar
BOOT-INF/lib/jedis-3.3.0.jar
BOOT-INF/lib/commons-pool2-2.8.1.jar
BOOT-INF/lib/janino-3.1.4.jar
BOOT-INF/lib/commons-compiler-3.1.4.jar
BOOT-INF/lib/commons-io-2.6.jar
(4). 添加jar文件到BOOT-INF/lib/目录里
# 1. 解压jar
infinova@lixin Desktop % jar -xvf app.jar
# 2. 业务操作(添加或删除jar包内容)
infinova@lixin Desktop % cp ../route-service-1.0.0-SNAPSHOT.jar ./BOOT-INF/lib/
# 3. 重新压缩生成新的jar
# -M : 不创清单文件
# -0 : 仅存储;不使用任何 ZIP 压缩
# -c : 创建新档案
# -f : 指定档案文件名
infinova@lixin Desktop % jar -cfM0 app-new.jar BOOT-INF/ META-INF/ org/