Reputation: 2752
Here is my code example:
private static void fac(Map<? extends Serializable,? extends Serializable> mapTo){
//do sth.
}
public static void main(String[] args) {
Map<String,Object> mapFrom=null;
fac((Map<? extends Serializable, ? extends Serializable>) mapFrom);
}
The code above is compiled successfully in eclipse(with a type safety warning) but failed in maven(an "incompatible types" error caused by javac?).
Now I have to change my code like this:
public static void main(String[] args) {
Map<String,Object> mapFrom=null;
fac((Map) mapFrom);
}
I've confirmed the java version is the same, and my question is:
Why do they have different behavior?
What's the preferred way to write the code?
Upvotes: 0
Views: 1670
Reputation: 328614
Eclipse comes with its own Java compiler; Maven uses javac
. Most of the time, both accept the same code but generics are complicated and compiler do have bugs. There are a couple of known bugs in javac
of Java 6 which cause problems, for example.
Oracle will not fix them. The solution is to use Java 7 to run Maven and configure the maven-compiler-plugin
the generate Java 6 byte code (see Kumar Sambhav's answer).
Upvotes: 1
Reputation: 7765
Try adding this plugin (inside build->plugins-> tag) to your pom.xml :-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
This will ensure the compiler version to be used with your project.
Upvotes: 0