Doro
Doro

Reputation: 355

Eclipse Maven and Java 8 problems

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>

Upvotes: 2

Views: 11638

Answers (4)

Jeff Cook
Jeff Cook

Reputation: 8784

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>

Upvotes: 3

Azhar Khan
Azhar Khan

Reputation: 4098

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.

Upvotes: 2

davidxxx
davidxxx

Reputation: 131356

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.

Upvotes: 5

Doro
Doro

Reputation: 355

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'

Upvotes: 4

Related Questions