Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Compilation Error: (use -source 7 or higher to enable diamond operator)

I'm using maven in IntelliJ, JDK1.8, maven 3.2.5. Got compilation error: use -source 7 or higher to enable diamond opera. details are as follows:

  [ERROR] COMPILATION ERROR :    [INFO] -------------------------------------------------------------   [ERROR] TrainingConstructor.java:[31,55] diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)   [ERROR] DTM.java:[79,21] try-with-resources is not supported in -source 1.5  (use -source 7 or higher to enable try-with-resources)   [ERROR] ticons.java:[53,44] diamond operator is not supported in -source 1.5  (use -source 7 or higher to enable diamond operator) 

Any suggestions? Is there any other configuration to set this -source level? seems it doesn't use java 1.8.

like image 252
HappyCoding Avatar asked Mar 25 '15 14:03

HappyCoding


People also ask

Is not supported in 1.5 use 7 or higher to enable diamond operator?

Maven Compilation Error: Use -source 7 or higher to enable diamond operator. This error happens only in Maven command line compile or in your IDE if you use new features of JDK that is not available outside 1.5. The reason is that MAVEN compiler default for source and target is 1.5 JDK version.

Why does Maven build failure in eclipse?

The error message indicates that Maven is unable to find the Java compiler, which comes only with a JDK and not with a JRE.


2 Answers

Check how your maven-compiler-plugin is configured, it should use java version 7 or higher:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-compiler-plugin</artifactId>     <version>3.1</version>     <configuration>         <source>1.7</source>         <target>1.7</target>     </configuration> </plugin> 

For a more complete answer see the one below.

like image 109
Sergey Pauk Avatar answered Oct 25 '22 01:10

Sergey Pauk


SOLUTION 1 - Set these properties in the pom.xml

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

SOLUTION 2 - Configure the Maven compiler plugin (always in the pom.xml)

<build>      <plugins>     <plugin>         <artifactId>maven-compiler-plugin</artifactId>         <configuration>             <source>1.7</source>             <target>1.7</target>         </configuration>     </plugin> </plugins> ... 

WHY IT HAPPENS

The problem arises because

[...] at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

Maven Compiler Plugin Introduction (until version 3.3)

and with recent Maven versions:

Also note that at present the default source setting is 1.6 and the default target setting is 1.6, independently of the JDK you run Maven with. You are highly encouraged to change these defaults by setting source and target as described in Setting the -source and -target of the Java Compiler.

Maven Compiler Plugin Introduction

That's why changing the JDK has no effect on the source level. So you have a couple of way to tell Maven what source level to use.

JDK VERSION TO USE?

If you set a target 1.7 like in this example be sure that the mvn command is actually launched with a jdk7 (or higher)

LANGUAGE LEVEL ON THE IDE

Usually IDEs use maven pom.xml file as a source of project configuration. Changing the compiler settings in the IDE not always has effect on the maven build. That's why, the best way to keep a project always manageable with maven (and interoperable with other IDEs) is edit the pom.xml files and instruct the IDE to sync with maven.

like image 25
Fabio Bonfante Avatar answered Oct 24 '22 23:10

Fabio Bonfante