記一次Intellij-IDEA配置JDK1.8,支持Lambda新特性

背景

  • Jdk 1.8支持Lambda新特性,想試試直接在IDEA使用,于是創(chuàng)建一個web項目,著手準(zhǔn)備使用時,發(fā)現(xiàn)了以下的問題。環(huán)境已經(jīng)中已經(jīng)配置了jdk1.8環(huán)境


    圖片1

解決方法

  1. 先打開ideaproject structure配置
    圖片2
  2. 其次將所有的模塊配置成為1.8環(huán)境,這樣保證它的語法檢測不報錯,但是編譯的時候他還是選擇idea的默認(rèn)配置,接下來更改編譯配置
圖片3
  1. 進入idea的設(shè)置頁面,將編譯模式改成1.8
圖片4

異常問題

如果缺少第三部,會出現(xiàn)如下錯誤

圖片5

FAQ:

設(shè)置完成之后,如果重新啟動IDEA時,就會發(fā)現(xiàn)所有的設(shè)置又重新恢復(fù)到默認(rèn)狀態(tài)(JDK 1.5)

原因是:

Apache Maven Compiler Plugin

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

解釋是:

該插件從3.0版本開始,
默認(rèn)編譯器是javax.tools.JavaCompiler (前提是JDK 1.6以后);
如果想使用javac,需要手動設(shè)置。
當(dāng)前(Version: 3.5.1),
默認(rèn)使用JDK 1.5解析和編譯源碼,
與運行Maven的JDK版本無關(guān)!

PS:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>

解決方法:

pom.xml中指定compiler的版本

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

當(dāng)然還有一個簡單寫法

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容