背景&问题描述:
工作中有需要利用logback实现日志脱敏处理,最后实现为:编写脱敏逻辑java代码编译成class文件,塞到logback-classic-1.2.3.jar 内,应用加载魔改后台的jar包配合logback配置文件实现。 随后发现并应用加载新jar包失败,追踪到类似如下代码:
第一段的jar是未修改的,正常运行。
第二段的jar是修改后的,line24 getMainAttributes 报错空指针,得知jarFile2.getManifest() 返回可能为空。
原因
我的理解就是破坏了jar包的完整性,需要重新打包。 摘自stackoverflow
It's a bug in JarInputStream that MANIFEST.MF must be in the first entries in a ZIP file, like this:
$ jar -tvf example-correct-archive.jar
623 Thu Jun 13 12:13:42 CEST 2019 META-INF/MANIFEST.MF
0 Thu Jun 13 12:13:42 CEST 2019 META-INF/
...
解决方式
通过如下操作使用jar命令重新打包。
a) Reorder entries in JAR file using jar tool:
- unpack
test.jarfile to directoryfoo - move existing
MANIFEST.MFfile one level above directoryfooand removeMETA-INFdirectory - inside
foodirectory run command:jar cvfm ../test.jar ../MANIFEST.MF . - outside of
fooyou will get fixed JAR file with correct entries order
b) use JarFile instead of JarInputStream