java 运行jar包时报错:无法打开某个引用的jar包,但在目录下可以找到相应的jar包,并且格式和内容正确,这就有可能是在maven打包时出现了错误。我个人是在maven的插件中引用了maven-shade-plugin插件,把其中springboot的jar同样再经过一层打包包装在了jar中,因此出现了在运行中找不到jar的问题。
[root@localhost testcpu]# java -jar agent_demo-0.0.1-SNAPSHOT-shaded.jar
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/spring-boot-starter-web-2.0.3.RELEASE.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/spring-boot-starter-web-2.0.3.RELEASE.jar'
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:254)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:239)
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/spring-boot-starter-web-2.0.3.RELEASE.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:282)
at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:262)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:250)
... 6 more
解决方法:把maven-shade-plugin插件注释掉选择其他的解决方案。