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

64,672

Solution 1

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

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

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

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

Share:
64,672
HappyCoding
Author by

HappyCoding

not yet

Updated on August 02, 2020

Comments

  • HappyCoding
    HappyCoding almost 4 years

    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.

  • HappyCoding
    HappyCoding about 9 years
    Thanks. I included them to POM under <build><plugins>. Some errors has gone. Still one compilation error: "Word.java:[17,22] > expected". the corresponding code is: List<String> defaultSW = Arrays.asList("a", "an", "and", "are"). Any idea?
  • S. Pauk
    S. Pauk about 9 years
    this line List<String> defaultSW = Arrays.asList("a", "an", "and", "are"); is correct, your problem is somewhere else
  • HappyCoding
    HappyCoding about 9 years
    Thanks. also tried with version 3.2 you suggested. still an error " java:[17,22] > expected ". the corresponding code fragment is: List<String> defaultSW = Arrays.asList("a", "an", "and", "are"). Are you aware of this problem?
  • HappyCoding
    HappyCoding about 9 years
    that's the only code I have in the java file. Any idea for this error message?
  • S. Pauk
    S. Pauk about 9 years
    I would suggest you to ask a new question and provide the sources cause the current question about maven is resolved.
  • Dave Newton
    Dave Newton about 9 years
    @HappyCoding And it's not the only code in the file if it's on line 17, character 22...
  • Dave Newton
    Dave Newton about 9 years
    Doesn't affect the Maven build, though.
  • pezetem
    pezetem about 9 years
    true, nevertheless I thought it was worth noticing, I had similar problem before, my pom.xml was set to use java 1.8 but i compiled code from intellij-idea and it took me a while to solve find the target bytecode version - it just a small hint
  • djb
    djb over 7 years
    if you use the >maven-javadoc-plugin plugin you must also configure it with the source level via <configuration> <source>1.7</source> </configuration> . Note that the 'site' goal will also run javadoc.
  • S. Pauk
    S. Pauk over 7 years
    @djb good point but I suppose that the original question was about the compile phase
  • djb
    djb over 7 years
    yes, but I put it here in case others have the problem. I hit the error message and a search brought me here, but I had 1.7 in the compiler plugin config so the original solution did not help. Digging found the javadoc config.
  • keinabel
    keinabel over 7 years
    I did exactly what you wrote and I still get that error