Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix '<>'operator is not allowed for source level 1.7

Tags:

java

eclipse

I am using JDK 1.8.0. When I import the code into Eclipse I am getting the error:

'<>'operator is not allowed for source level 1.7

Example:

List<String[]> errors = new ArrayList<>();

I am using Eclipse Kepler.

like image 889
Avinash Avatar asked Oct 24 '16 12:10

Avinash


4 Answers

Sometimes I have seen Eclipse become confused about the Java target version, and throw incorrect messages (even if the project is set up correctly to support Java 7). The easiest way to fix it is to change the target version, then change it back to the expected target version.

This version can be checked by opening the Project properties dialog (right click on the project, and select Properties), and check the settings on two tabs:

  1. Java Compiler tab: Check whether there are any specific settings such as always use a JDK version, etc. By default, all settings here are set to "Use compliance from execution environment 'JavaSE-1.x' on the 'Java build path'." (where x is a Java version). If a specific Java version (pre-Java 7) is selected here, then select the one you are targeting, and you should be done. If the previously mentioned checkbox is set, then follow to step 2.
  2. Java build path tab: Go to libraries, and edit the JRE system library accordingly.

Again, if both settings seem correct, change the latter, rebuild your project (e.g. close your dialog), then change it back and rebuild.

Remarks: if you are using a Maven/Gradle project, it is possible that you should edit these settings in the Maven/Gradle configuration.

like image 74
Zoltán Ujhelyi Avatar answered Nov 15 '22 16:11

Zoltán Ujhelyi


It needs to update the value of "Compiler compliance level" to 1.7 or higher in eclipse. Steps are as below:

  1. Open project in eclipse
  2. Right click on Project and go to Properties
  3. In Properties panel: click "Java Compiler" at the left and update the value of "Compiler compliance level" to 1.7 or higher from the drop down.
  4. Click Apply and OK button enter image description here
like image 42
Ripon Al Wasim Avatar answered Nov 15 '22 14:11

Ripon Al Wasim


Add this to your pom.xml, below <project>.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Most likely it's the default Eclipse Maven configuration that set your project's JDK version to 1.5 - God knows why. This will set it to 1.8 instead.

like image 5
Hung Luong Avatar answered Nov 15 '22 16:11

Hung Luong


Upgrade to Eclipse Mars or latest version Neon :

https://www.eclipse.org/downloads/

like image 1
Chirag Parmar Avatar answered Nov 15 '22 14:11

Chirag Parmar