错误:java:错误:无效的源发布:13将JDK12与IntelliJ一起使用 [英] Error:java: error: invalid source release: 13 using JDK12 with IntelliJ
问题描述
我正在尝试使用JDK-12 ea构建一个项目. 尝试执行示例类时:
I am trying to build a project with JDK-12 ea. While trying to execute a sample class:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
// After JEP-325
switch (value) {
case 1 ->System.out.println("one");
case 2 ->System.out.println("two");
default ->System.out.println("many");
}
}
IDE抛出读取错误
错误:java:错误:无效的源发行版:13
Error:java: error: invalid source release: 13
相关项目配置屏幕:
模块设置
SDK
编译器设置
关于IDE:
IntelliJ IDEA 2018.3.3 (Community Edition)
Build #IC-183.5153.38, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
试图将语言级别切换回12,没有实验性功能,但最终以
Tried switching back the language level to 12, without experimental features, but it ends in
我记得最近成功使用了实验性功能(不确定确切的IntelliJ版本).除此以外,我还需要配置一些更改吗?
I remember using the experimental features in recent past(not sure of exact IntelliJ version) successfully. Has something changed that I need to configure apart from this as well?
推荐答案
正确的方法™
支持开关表达式的IntelliJ最早版本是2019.1版本,该版本于2019年3月27日发布.您可以在此处获取: https://www.jetbrains.com/idea/download .当然,您还需要JDK 12,并将其设置为项目SDK.
The right way™
The earliest version of IntelliJ that supports switch expressions is version 2019.1, which was released on March 27, 2019. You can get it here: https://www.jetbrains.com/idea/download . You also need JDK 12 of course, and set that as your project SDK.
然后您可以将语言级别设置为:
Then you can set the language level to:
12 (Preview) - Switch expressions
有了它,一切都会正常工作.
With that, everything should work.
如果没有,则可能要检查您是否已经拥有;
If it doesn't, you might want to check that You've;
- 将项目语言级别以及模块语言级别设置为"12(预览)"
- 在应用程序的运行配置中将执行JRE设置为12(或默认值,这是项目SDK).
- 在设置"中设置正确的字节码版本-> 构建,执行,部署"/编译器/Java编译器. (将此字段保留为空以匹配语言级别)
将我的评论变成答案.您可以通过以下操作将--enable-preview
标志添加到VM:
Turning my comment into an answer. You can add the --enable-preview
flag to the VM by going to:
运行-> 编辑配置...
然后从左侧的树菜单中选择您的主类,然后将--enable-preview
粘贴到"VM options"(虚拟机选项)中.盒
Then selecting your main class from the tree menu on the left, and pasting --enable-preview
in the "VM options" box
您可以通过以下步骤对编译器执行相同操作:
You can do the same for the compiler by going to:
文件-> 设置...
然后在树菜单中的 Build,Execution,Deployment ->下. 编译器-> Java编译器,您可以将--enable-preview
放入其他命令行参数"框:
Then in the tree menu under Build, Execution, Deployment -> Compiler -> Java Compiler you can put --enable-preview
in the "Additional command line parameters" box:
请注意,执行此操作后,Intellisense仍然可能无法正常工作.我仍然在->
下看到红色的波浪线,并显示错误消息意外的令牌".但是,当我单击运行按钮时,该类将编译并运行正常.
Note that intellisense still might not work after doing that. I'm still seeing red squiggly lines under the ->
s with the error message "unexpected token". But, when I click the run button the class compiles and runs just fine.
这篇关于错误:java:错误:无效的源发布:13将JDK12与IntelliJ一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!