IntelliJ搭配JDK10与spring-boot-starter-test启动报错ExceptionInInitializerError
解决Spring Boot Starter Test与JDK 10兼容问题的方案
嘿,我之前也碰到过类似的坑,结合你的配置和报错信息来看,确实是Spring Boot 2.0.x版本对JDK 10的支持不完善导致的,再加上IntelliJ的编译配置可能没跟上版本,才触发了这个ExceptionInInitializerError。给你几个靠谱的解决办法:
1. 优先升级Spring Boot版本到支持JDK 10的正式版
Spring Boot 2.0.x系列只是半兼容JDK 10,测试模块里的不少兼容性问题在2.1.x及以后的版本才被彻底修复。你只需要把父依赖的版本改成2.1.0.RELEASE或者更高的稳定版就行:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> </parent>
这个办法是最彻底的,升级后不仅测试模块的问题能解决,还能拿到JDK 10的完整支持。
2. 检查并修正IntelliJ的编译配置
有时候IDE的配置没跟上JDK版本,也会导致这类莫名其妙的编译错误:
- 打开
File → Project Structure → Project,确认Project SDK是JDK 10.0.1,Project language level设置为10 - Local variable type inference。 - 接着去
File → Settings → Build, Execution, Deployment → Compiler → Java Compiler,确保每个Module的Target bytecode version都是10,并且勾选Use '--release' option for cross-compilation (Java 9 and later)。
3. 临时排除冲突依赖(如果暂时无法升级Spring Boot)
如果因为项目限制不能马上升级版本,可以尝试排除spring-boot-starter-test里和JDK 10冲突的依赖,比如旧版的JUnit Vintage引擎:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
不过这只是临时的 workaround,还是建议尽快升级Spring Boot版本从根源解决问题。
4. 清理IntelliJ缓存并重启
有时候IDE缓存损坏也会导致奇怪的编译错误,试试:
点击顶部菜单栏的File → Invalidate Caches...,选择Invalidate and Restart,等IDE重启后重新构建项目,说不定问题就消失了。
内容的提问来源于stack exchange,提问作者Kavzor
相关产品推荐
相关产品推荐

