IntelliJ 2021.2.2编译Java项目报错invalid source release: 18求助
Executing pre-compile tasks... Loading Ant configuration... Running Ant tasks... Running 'before' tasks Checking sources Parsing java... [Adexo] java: error: invalid source release: 18 Checking dependencies... [Adexo] Dependency analysis found 0 affected files Errors occurred while compiling module 'Adexo' javac 11 was used to compile java sources Finished, saving caches... Module 'Adexo' was fully rebuilt due to project configuration/dependencies changes Compilation failed: errors: 1; warnings: 0 Executing post-compile tasks... Loading Ant configuration... Running Ant tasks... Synchronizing output directories... 30.9.2021 22:34 - Build completed with 1 error and 0 warnings in 3 sec, 279 ms



解决方案
该报错由IntelliJ IDEA更新后自动修改了项目JDK相关配置,和实际使用的JDK 16.0.1版本不匹配导致,按以下步骤逐一核对修改即可:
- 核对项目级别JDK配置
打开File > Project Structure > Project页面,确认两个配置项:SDK选项选择本地安装的JDK 16.0.1,排除IDEA自动切换为内置JDK或其他版本的可能Language level选项选择对应JDK 16的等级,不要选17、18等更高版本
- 核对模块级别JDK配置
在Project Structure页面切换到Modules标签,选中Adexo模块:Sources标签下的Language level和项目级配置保持一致,为JDK 16对应等级Dependencies标签下的模块SDK也选择JDK 16.0.1
- 核对Java编译器配置
打开File > Settings > Build, Execution, Deployment > Compiler > Java Compiler:Project bytecode version选择16,不要使用默认的18或其他版本- 下方
Per-module bytecode version列表如果存在Adexo模块的配置,将其Target bytecode version也改为16 - 取消勾选
Use '--release' option for cross-compilation (Java 9 and later)选项,避免IDEA自动追加错误的版本参数
- 清理缓存重启重建
点击顶部菜单File > Invalidate Caches...,勾选Clear file system cache and local history后点击Invalidate and Restart,重启后重新构建项目即可正常编译。
内容的提问来源于stack exchange,提问作者ADEX
相关产品推荐
相关产品推荐

