eclipse 如何修复源级别 1.7 不允许使用“<>”运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40217983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix '<>'operator is not allowed for source level 1.7
提问by Avinash
I am using JDK 1.8.0. When I import the code into Eclipse I am getting the error:
我正在使用 JDK 1.8.0。当我将代码导入 Eclipse 时,出现错误:
'<>'operator is not allowed for source level 1.7
'<>'运算符不允许用于源级别 1.7
Example:
例子:
List<String[]> errors = new ArrayList<>();
I am using Eclipse Kepler.
我正在使用Eclipse 开普勒。
回答by Zoltán Ujhelyi
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.
有时我看到 Eclipse 对 Java 目标版本感到困惑,并抛出不正确的消息(即使项目设置正确以支持 Java 7)。修复它的最简单方法是更改目标版本,然后将其更改回预期的目标版本。
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:
可以通过打开项目属性对话框(右键单击项目,然后选择Properties
)并检查两个选项卡上的设置来检查此版本:
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.Java build path
tab: Go to libraries, and edit the JRE system library accordingly.
Java Compiler
选项卡:检查是否有任何特定设置,例如始终使用JDK版本等。默认情况下,此处的所有设置都设置为“在'Java构建路径'上使用执行环境'JavaSE-1.x'的合规性”。(其中 x 是 Java 版本)。如果在此处选择了特定的 Java 版本(Java 7 之前),则选择您要定位的版本,您应该就完成了。如果前面提到的复选框已设置,则按照步骤 2 操作。Java build path
选项卡:转到库,并相应地编辑 JRE 系统库。
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.
备注:如果您使用的是 Maven/Gradle 项目,您可能应该在 Maven/Gradle 配置中编辑这些设置。
回答by Hung Luong
Add this to your pom.xml, below <project>
.
将此添加到您的 pom.xml 中,如下所示<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.
很可能是默认的 Eclipse Maven 配置将项目的 JDK 版本设置为 1.5 - 天知道为什么。这会将其设置为 1.8。
回答by Ripon Al Wasim
It needs to update the value of "Compiler compliance level" to 1.7 or higher in eclipse. Steps are as below:
需要在eclipse中将“Compiler compliance level”的值更新为1.7或更高。步骤如下:
回答by Chirag Parmar
Upgrade to Eclipse Mars or latest version Neon :
升级到 Eclipse Mars 或最新版本 Neon :
回答by Morten Finnerud
Inserting 8 in the section of the pom (maven) helped in my case.
在 pom (maven) 的部分插入 8 对我有帮助。
回答by ansanes
Try the followings:
请尝试以下操作:
List<String[]> errors = new ArrayList();
OR,
或者,
List<String[]> errors = new ArrayList<String[]>();
Or, change your project Compiler compliance level to 1.7 or higher in Eclipse.
或者,在 Eclipse 中将您的项目编译器合规性级别更改为 1.7 或更高。