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>

 

轉載請注明出處:http://www.cnblogs.com/keitsi/p/5457699.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM