
springboot jar包瘦身后启动提示 illegalaccesserror ?
在对 spring boot 应用进行瘦身后,启动 jar 包时可能会遇到 java.lang.illegalaccesserror 错误,具体表现为 class org.springframework.cloud.openfeign.hystrixtargeter$$enhancerbyspringcglib$$7e887a8a cannot access its superclass org.springframework.cloud.openfeign.hystrixtargeter。
这通常是由在瘦身后的 classloader 中获取父类时出现不一致造成的。具体原因可能是:
- 使用了 spring boot devtools,但项目本身未依赖该 jar。
- 在 manifest.mf 添加了 class-path 参数,指定瘦身 jar 之外的依赖库。
解决方案:
一种临时的解决方法是通过自定义 beanpostprocessor 来还原 classloader,但这只是治标不治本。
永久的解决方案是:
- 移除 spring boot maven 插件:org.springframework.boot:spring-boot-maven-plugin。
- 在 maven dependency 插件中添加 main-class 和 outputdirectory:
org.apache.maven.plugins maven-jar-plugin true lib/ false com.example.Application ${project.build.directory}/boot-jar
完成以上修改后,即可解决 illegalaccesserror 错误,实现 jar 包瘦身。










