22

I have just started working on a Java project, and have downloaded the source code from GitHub, using IntelliJ- I have never used IntelliJ before, but am told that it is a much better IDE to use than Eclipse (which is what I was using when I last did any Java development- about four years ago).

When I try to build the source locally on my computer, having pulled the latest working version from GitHub, I get a compile error on several different lines of code- the error says:

Error:(27, 34) java: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)

and the lines where these compile errors appear, are lines like:

return new ArrayList<>(0);

If I select the line, and do Alt + Enter on the error, it shows a message stating that I can

"Set language level to 7- Diamonds, ARM, Multi-cache, etc"

However, if I select this option, nothing happens...

In the pom.xml file, there is the following xml:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>${maven-source-plugin.version}</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

But when I looked this error up, I came across the answer at: Diamond type are not supported at this language level, which indicated that I should be using maven1.7 or higher- and it appears that the project is already using version 1.8, so I don't understand why I'm getting this compile error...

Anyone have any suggestions?

Community
  • 1
  • 1
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

2 Answers2

49

Add the following code into your pom.xml file.

<!-- maven-compiler-plugin -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
grovina
  • 2,999
  • 19
  • 25
user8936069
  • 506
  • 5
  • 2
10

Under File->Project Structure (Ctrl + Alt + Shift + s) there's a section called Project Settings, and Project

Here you can set the project language level. enter image description here

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
DrLime2k10
  • 276
  • 1
  • 2
  • 14
  • 2
    Thanks for your answer. I just tried setting that to 7, clicked 'Apply', then 'OK', and then tried building the project again, but I get the same errors in the console: `Error:(27, 34) java: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)` – Noble-Surfer May 16 '17 at 15:16
  • The Project SDK is 1.8 in case that makes a difference...? – Noble-Surfer May 16 '17 at 15:18
  • Thanks for the link- I've just followed through the accepted answer, and my project all seems to be set up correctly according to what it says... The only thing I haven't done there is to reimport all Maven projects... I'm not sure how I get the `Maven Projects` Tool Window to be displayed? – Noble-Surfer May 16 '17 at 15:48
  • Ok, so I've just done the reimport, but I'm still getting the same compile errors... any other suggestions? – Noble-Surfer May 16 '17 at 16:00
  • Looking through the accepted answer on the question you linked to, in the first screenshot, uner "Per-module bytecode version", the screenshot shows something called `testNgMavenExample`... I don't have anything there in mine- should I have? I tried clicking the '+' to add a module, but the 'Choose Module' window that popped up said "nothing to show".... do I need to download a module to use or something? – Noble-Surfer May 17 '17 at 08:22