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

本文详细介绍了在IDEA中遇到的关于try-with-resources特性不支持的错误及其解决方法。通过调整项目的SourceLanguageLevel和TargetLanguageLevel至1.8,成功解决了编译错误,使项目能够正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误描述

在用IDEA编译别人的项目的时候遇到下面的错误:

java: try-with-resources is not supported in -source 1.6
  (use -source 7 or higher to enable try-with-resources)

按词面理解是编译器抱怨说 source 1.6 不支持 try-with-resources 特性, 需要启用该特性要设置 source 1.7 或更高的版本

 

解决办法

  • 设置当前模块的 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

  • 设置当前模块的 Target Language Level:

File -> Settings -> File | Settings | Build, Execution, Deployment -> Compiler -> Java Compiler -> Per-module bytecode version -> Target bytecode version

选择 1.8

 

再重新编译,OK一切正常了~

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟子涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值