2016-08-14 87 views
1

我從簡單的Web應用程序遷移到Maven Web應用程序和Eclipse霓虹燈我遇到了令人沮喪的問題:在我添加了pom.xml規範以使用Java 8或7後,它不起作用。Eclipse Maven和Java 8問題

爲了驗證它是否有效,我寫了一個簡單的類,我使用try(表達式)聲明。

我該怎麼做才能在maven中使用Java 8(我已經安裝了,它在普通的web應用程序中工作)?

pom.xml中的代碼是這樣的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.webdata</groupId> 
    <artifactId>WebParser</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>WebParser</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <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> 

    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
      <groupId>net.sourceforge.htmlunit</groupId> 
      <artifactId>htmlunit</artifactId> 
      <version>2.23</version> 
     </dependency> 
    </dependencies> 
</project> 
+0

檢查這[出](http://stackoverflow.com/questions/15027255/eclipse-java-8-support) –

+0

我使用eclipse霓虹燈...我看到它是開普勒? – Doro

+0

java 8不支持ur eclipse?你有嘗試的代碼,如lambda或列表集等 –

回答

1

首先,你JAVA_HOME畝st指向JDK 1.8。
否則,如果不是這樣,你必須指定一個JDK 1.8的源和目標在你pom.xml在這樣的編譯器配置:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <configuration> 
     <source>1.8</source> 
     <target>1.8</target> 
     <compilerVersion>1.8</compilerVersion>  
     <fork>true</fork> 
     <executable>D:\jdk1.8\bin\javac</executable>     
    </configuration> 
</plugin> 

然後,在Eclipse中,你必須檢查偏好:

  • Java->Installed JREs安裝了1.8 JRE/JDK。

你可以設置以下爲默認值,如果要使用1.8在Eclipse的任何新創建的項目:

  • Java->Installed JREs:選擇1.8。
  • Java->Compiler:選擇JDK編譯器合規性級別爲1.8。

如果默認首選項不使用1.8編譯合規性和JDK/JRE或創建項目外,從這個Eclipse中設置爲1.8的編譯合規性和JDK/JRE的喜好,你應該檢查和調整可能您的Eclipse項目的properties
要做到這一點,請進入項目屬性,然後選擇Java Build PathLibraries選項卡。您必須檢查JRE System Library使用Java 1.8。如果情況並非如此,請使用1.8版本進行更換。
完成後,始終在項目的屬性中,轉至Java Compiler並檢查是否使用JDK編譯器符合Java 1.8。

它應該工作。

+0

這不適合我爲什麼..... –

+0

@srinivas gowda它值得downvote? – davidxxx

2

這很有趣:))

我搜索3-4小時,我試過一些方法,只有經過我在這裏問我找到了解決辦法:)

右鍵單擊項目 - >屬性 - > Java編譯器 - >取消選中 '從執行中使用compilance ......',然後選擇 '1.8'

+0

Maven更新後(Alt + F5)你必須再次改變設置... – mortalis