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

JavaMavenIntellij IdeaMaven 3

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

You have to change your configuration:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

You should learn the difference between source/taget option in JavaC and the usage of JDK 1.8/1.7 etc.

Apart from that you should upgrade the use maven-compiler-plugin.

Solution 4 - Java

If You already tried the @Sergey Pauk and @khmarbaise solution, take a look also in the settings -> Build, Execution, Deployment -> Compiler -> Java Compiler, there are target bytecode versions for particular modules

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHappyCodingView Question on Stackoverflow
Solution 1 - JavaS. PaukView Answer on Stackoverflow
Solution 2 - JavaFabio BonfanteView Answer on Stackoverflow
Solution 3 - JavakhmarbaiseView Answer on Stackoverflow
Solution 4 - JavapezetemView Answer on Stackoverflow