IDEA 编译错误:java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)
错误描述
在用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一切正常了~
更新 2016/08/10
虽然按照上面步骤设置之后是可以临时去掉这个报错,但是一段时间后发现这配置总会被自动的又改回去,好郁闷!
观察发现每次更新pom文件IDEA都会自动地更新Target bytecode version为1.5,猜测原因可能是在pom配置文件没有指定要使用那个版本的JDK所以IDEA只能默认给你指定一个。
要彻底解决这个问题只需在pom文件中配置maven-compiler-plugin并指定编译器的版本为你想要的版本即可:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build>
Keep it simple!
分类:
Java/Scala
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性