Java generics: compiler error not shown in eclipse

I have the following classes:

public class EntityDataModel<T extends AbstractEntity> { ... } public abstract class BarChartBean<E extends ChartEntry, T> { protected EntityDataModel<? extends T> currentModel; ... } 

I can compile and run this code on eclipse without problems, but when I call mvn compile , this error occurs:

 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project edea2: Compilation failure: Compilation failure: [ERROR] C:\Develop\...\BarChartBean.java:[53,30] error: type argument ? extends T#1 is not within bounds of type-variable T#2 [ERROR] where T#1,T#2 are type-variables: [ERROR] T#1 extends Object declared in class BarChartBean [ERROR] T#2 extends AbstractEntity declared in class EntityDataModel 

The error is pretty clear, and, theoretically, javac is right, and the eclipse compiler is wrong.

Why is there such a difference?

Here you will find environmental information:

  • Eclipse

    • Release Mars.2 (4.5.2)
    • jdk 1.8.0_71
    • Compiler Compliance Level: 1.8
    • Errors / Warnings
  • Maven

    • Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13: 57: 37 + 02: 00)
    • Maven home: C: \ Develop \ tools \ apache-maven-3.3.3
    • Java Version: 1.8.0_71, Supplier: Oracle Corporation
    • Java Homepage: C: \ Program Files \ Java \ jdk1.8.0_71 \ jre
    • Default language: it_IT, platform encoding: Cp1252
    • OS name: "Windows 10", version: "10.0", arch: "amd64", family: "dos"
    • Maven compiler plugin:

       <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <showDeprecation>true</showDeprecation> <showWarnings>true</showWarnings> </configuration> </plugin> 

Question How can I align the behavior of the eclipse compiler with javac (but I do not want to use javac in eclipse)?

+8
java eclipse maven maven-3 maven-compiler-plugin
source share
1 answer

This is another mismatch between the Java Eclipse compiler and the official JDK compiler (because it really is) . And javac is not always the right actor in this game, you can really hit a javac error not found in the Eclipse compiler.

A similar problem has already been reported: Error 456459 : mismatch between the Eclipse compiler and javac - enums, interfaces, and generics .

To align Maven with Eclipse, you can configure maven-compiler-plugin as follows:

 <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> <compilerId>eclipse</compilerId> </configuration> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-compiler-eclipse</artifactId> <version>2.7</version> </dependency> </dependencies> </plugin> 

Basically, you tell Maven to use the Java Eclipse compiler. I was able to reproduce your problem and applying this configuration, the Maven build was fine. However, I would not recommend this approach.

Configuring Eclipse to use the JDK compiler, on the other hand, is a bit more complicated, mainly because the Eclipse compiler is part of the IDE functions. The procedure is explained in the q / a stack overflow: How to start Javac from Eclipse .

+6
source share

All Articles