IDEA 编译错误:java: try-with-resources is not supported in -source 1.5

错误描述:

Error:(59, 20) java: -source 1.5 中不支持 try-with-resources (请使用 -source 7 或更高版本以启用 try-with-resources)

这里说明编译版本太低,需要更新到jdk1.7或者更高。

解决方法:

一、修改pom.xml

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
          <source>1.8</source>
          <target>1.8</target>
     </configuration>
</plugin>

二、修改IDEA配置,默认就使用1.8版本 

点击File -> Settings -> Build,Execution,Deployment -> Compiler -> Java Compiler 
Project bytecode version下拉框选择1.8。 
该选项不会生效,因maven项目并没有使用此处指定的编译器。

以上方法若还是不能解决您问题的话,可以尝试以下的方法:

 

1、设置当前模块的 Source Language Level:
File -> Project Structure -> Modules -> Sources -> Language Level

选择 8 - Lambdas, type annotations etc.

设置完成之后没有了之前的那个错误了,但是有可能出现了另一个错误:

Error:java: javacTask: source release 1.8 requires target release 1.8

报错信息表达编译器需要的source已经是1.8了,但同时target也要设置为1.8

2、设置当前模块的 Target Language Level:
File -> Settings -> File | Settings | Build, Execution, Deployment -> Compiler -> Java Compiler -> Per-module bytecode version -> Target bytecode version 选择 1.8


最后重新编译一下,再运行项目~

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值