错误描述:
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
最后重新编译一下,再运行项目~