2

I moved from simple web app to maven web app and with Eclipse Neon I encountered a frustrating problem: after I add in pom.xml the specification to use Java 8 or 7, it doesn't work.

To verify if it works I write a simple class where I use a try(expression) declaration.

What should I have to do to use Java 8 in maven (I have installed and it works in normal web app)?

The code of pom.xml is this:

<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>
Doro
  • 335
  • 2
  • 6
  • 24
  • check this [out](http://stackoverflow.com/questions/15027255/eclipse-java-8-support) – Bibek Shakya Aug 14 '16 at 22:07
  • I'm using eclipse Neon... I see that it is for Kepler? – Doro Aug 14 '16 at 22:10
  • java 8 is not supported by ur eclipse??? have you try it by code like lambda or List Set etc – Bibek Shakya Aug 14 '16 at 22:16
  • I'm using latest version which it works only if I have Java 8 – Doro Aug 14 '16 at 22:16
  • I know neon only run on java 8, have you set up java 8 or download java 8. do you set up your eclipse for java 8 by `project>properties>compiler` – Bibek Shakya Aug 14 '16 at 22:20
  • 1
    Yes, this is the solution :) ... I found that here: http://stackoverflow.com/questions/14804945/maven-build-path-specifies-execution-environment-j2se-1-5-even-though-i-chang but you was faster like me to write :) ... thanks anyway for helping :D – Doro Aug 14 '16 at 22:22

4 Answers4

5

First, your JAVA_HOME must point to a JDK 1.8.
Else if is not the case, you must specify a JDK 1.8 as source and target in the compiler configuration of your pom.xml in this way :

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

Then, in eclipse, you must check in Preferences that :

  • Java->Installed JREs has a 1.8 JRE/JDK installed.

You could set the following as default if you want to use the 1.8 for any new created projects in your Eclipse :

  • Java->Installed JREs : select the 1.8.
  • Java->Compiler : select the JDK compiler compliance level to 1.8.

If the default preferences don't use 1.8 compilation compliance and JDK/JRE or that the project was created outside from this Eclipse with preferences set to 1.8 compilation compliance and JDK/JRE, you should check and maybe adjust properties of your Eclipse project
To do it, go in the properties of your project, then Java Build Path and in the Libraries tab. You must check that the JRE System Library uses Java 1.8. If it is not the case, remplace it with the 1.8 version.
When it is done, always in the properties of your project, go to Java Compiler and check that you use JDK compiler compliance with Java 1.8.

It should work.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
4

It's funny :))

I searched 3-4 hours and I tried few methods and only after I asked here I found the solution :)

Right click on the project -> Properties -> Java Compiler -> uncheck 'Use compilance from execution...' and choose '1.8'

Doro
  • 335
  • 2
  • 6
  • 24
3

In pom.xml, defined this maven.compiler.source properties to tell Maven to use Java 8 to compile the project.

Maven Properties Java 8

<properties>
     <maven.compiler.target>1.8</maven.compiler.target>
     <maven.compiler.source>1.8</maven.compiler.source>
</properties>

Compiler Plugin - Alternative, configure the plugin directly.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186
2

This worked for me:

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

as mentioned here -> maven-compiler-plugin

Don't forget to update Maven project -> Alt + F5.

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32