已安装Java 11但编译仍用1.8.0_242,如何切换至Java 11?
解决Java版本切换问题(从JDK8切换到JDK11)
异常信息
an exception has occurred in the compiler (1.8.0_242-release). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
以下是具体切换步骤:
1. 配置系统环境变量
- 找到你的JDK11安装路径,比如Windows下通常是
C:\Program Files\Java\jdk-11.x.x,macOS/Linux下是/Library/Java/JavaVirtualMachines/jdk-11.x.x/Contents/Home - 修改系统的
JAVA_HOME环境变量,将其值设置为JDK11的安装路径 - 更新
PATH环境变量,把JDK11的bin目录(比如C:\Program Files\Java\jdk-11.x.x\bin)移到最顶部,确保系统优先调用JDK11的命令 - 验证配置:打开新终端,执行
java -version和javac -version,确认输出显示JDK11版本
2. 修正Android Studio的JDK配置
- 打开Android Studio,依次进入
File > Project Structure > SDK Location - 在
JDK Location选项中,选择JDK11的安装路径,点击OK保存 - 检查模块级配置:进入
File > Project Structure > Modules,选中你的项目模块,切换到Sources标签,将Language level设置为11 - Local variable syntax for lambda parameters;再切换到Dependencies标签,将Module SDK选择为JDK11
3. 配置Flutter使用的JDK
- 临时生效:在终端中执行对应系统的命令:
- Windows:
set JAVA_HOME=C:\Program Files\Java\jdk-11.x.x - macOS/Linux:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.x.x/Contents/Home
- Windows:
- 永久生效:
- Windows:通过系统环境变量窗口修改
JAVA_HOME(和步骤1一致) - macOS/Linux:编辑
~/.bash_profile或~/.zshrc文件,添加上述export命令,保存后执行source ~/.bash_profile(或source ~/.zshrc)使配置生效
- Windows:通过系统环境变量窗口修改
- 重新执行
flutter doctor -v,确认输出中的Java版本已更新为JDK11
4. 清理缓存并重启
- 执行
flutter clean清理项目缓存 - 重启Android Studio和终端,确保所有配置生效
内容的提问来源于stack exchange,提问作者axis-medias
相关产品推荐
相关产品推荐

