Java Eclipse:语法错误,参数化类型仅当源级别为 1.5 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2419226/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:21:34  来源:igfitidea点击:

Eclipse: Syntax Error, parameterized types are only if source level is 1.5

javaeclipse

提问by Thang Pham

I try to use the following code:

我尝试使用以下代码:

ArrayList<String>
Map<String, String>

Eclipse complains about both of them: Syntax Error, parameterized types are only if source level is 1.5.

Eclipse 对它们都抱怨:语法错误,参数化类型仅当源级别为 1.5 时。

I use JDK 1.6, but even if I go to compiler and switch the compliance to 1.5. It still give me this error. Any Idea why?

我使用 JDK 1.6,但即使我转到编译器并将合规性切换到 1.5。它仍然给我这个错误。任何想法为什么?

采纳答案by Omry Yadan

My guess would be that while you run eclipse itself with JDK 1.6, it's actually configured with a different default jre.

我的猜测是,当您使用 JDK 1.6 运行 eclipse 本身时,它实际上配置了不同的默认 jre。

See Window->preferences->java->Installed JREsand make sure that the checked JRE is 1.6.

查看Window->preferences->java->Installed JREs并确保检查的 JRE 是1.6.

If the default JRE is indeed 1.6, chances are that it's a project specific setting. See that the project is configured to use the right JRE.

如果默认 JRE 确实是 1.6,那么它很可能是项目特定的设置。查看项目是否配置为使用正确的 JRE。

回答by stacker

Right click your project and choose properties in the properties dialog check the Java Compiler settings, maybe you have different workspace settings.

右键单击您的项目并在属性对话框中选择属性检查 Java 编译器设置,也许您有不同的工作区设置。

回答by Jeff

Yes. Regardless of what anyone else says, Eclipse contains some bug(s) that sometimes causes the workspace setting (e.g. 1.6 compliant) to be ignored. This is even when the per-project settings are disabled, the workspace settings are correct (1.6), the JRE is correctly set, there is only a 1.6 JRE defined, etc., all the things that people generally recommend when questions about this issue are posted to various forums (as they often are).

是的。不管其他人怎么说,Eclipse 包含一些有时会导致工作区设置(例如 1.6 兼容)被忽略的错误。这甚至在每个项目设置被禁用、工作区设置正确(1.6)、JRE 设置正确、仅定义了 1.6 JRE 等时,所有这些都是人们在对这个问题提出疑问时普遍推荐的发布到各种论坛(因为它们经常是)。

We hit this irregularly, but often, and typically when there is some unrelated issue with build-time dependencies or other project issues. It seems to fall into the general category of "unable to get Eclipse to recognize reality" issues that I always attribute, rightly or wrongly, to refresh issues with Eclipse' extensive metadata. Eclipse metadata is a blessing and a curse; when all is working well, it makes the tool exceedingly powerful and fast. But when there are problems, the extensive caching makes straightening out the issues more difficult - sometimes much more difficult - than with other tools.

我们不定期地,但经常地,通常是当存在与构建时依赖项或其他项目问题无关的问题时。它似乎属于“无法让 Eclipse 识别现实”问题的一般类别,我总是正确或错误地归因于刷新 Eclipse 广泛元数据的问题。Eclipse 元数据是福也是祸;当一切正常时,它会使该工具变得非常强大和快速。但是当出现问题时,与使用其他工具相比,广泛的缓存会使解决问题更加困难 - 有时要困难得多。

回答by Jeff

I was able to solve this in Eclipse Helios by right-clicking on the project, picking "Java Build Path", "Add Library...", "JRE System Library", "Workspace default JRE (jdk1.6.0_17)" and finally "Finish".

我能够在 Eclipse Helios 中解决这个问题,方法是右键单击项目,选择“Java 构建路径”、“添加库...”、“JRE 系统库”、“工作区默认 JRE (jdk1.6.0_17)”和最后“完成”。

回答by Steve

Be wary, too, of inheriting workspace settings in your projects for Java Compiler/Compliance settings. I had a project that was imported into Eclipse. Eclipse generated the project with a J2SE 1.4 JRE, compiler, and compliance settings.

也要注意在项目中为 Java 编译器/合规性设置继承工作区设置。我有一个导入到 Eclipse 中的项目。Eclipse 使用 J2SE 1.4 JRE、编译器和合规性设置生成了项目。

When I went in and manually updated these settings and un-checked the "Enable project specific settings" box, I was still receiving build errors relating to 1.5 level compliance (even though the workspace settings were clearly 1.6). It wasn't until I re-checked the "Enable project specific settings" box and manually set the compiler/compliance levels to 1.6 did the errors go away.

当我进入并手动更新这些设置并取消选中“启用项目特定设置”框时,我仍然收到与 1.5 级合规性相关的构建错误(即使工作区设置显然是 1.6)。直到我重新选中“启用项目特定设置”框并手动将编译器/合规性级别设置为 1.6,错误才消失。

回答by Chris Blyseth

This workaround is helpful when this problem pops up.

出现此问题时,此解决方法很有帮助。

1) Set Workspace JRE to 1.5 (and apply/save). Projects will rebuild.

1) 将 Workspace JRE 设置为 1.5(并应用/保存)。项目将重建。

2)Set Workspace JRE back to 1.6 (and apply/save). Projects will rebuild.

2) 将 Workspace JRE 设置回 1.6(并应用/保存)。项目将重建。

All errors are then gone (for now at least).

然后所有错误都消失了(至少现在是)。

回答by JHC

I have experienced the same problem with Eclipse Indigo release - even though the project set up was correct. The main cause I suspect is that the workspace is defined in a separate folder from the actual source folders (as mentioned user748337). To fix the problem, I had to enable the project specific settings option (in the Porject preference) although it wasn't different from the workspace settings.

我在 Eclipse Indigo 版本中遇到了同样的问题 - 即使项目设置是正确的。我怀疑的主要原因是工作区是在与实际源文件夹不同的文件夹中定义的(如 user748337 所述)。为了解决这个问题,我必须启用项目特定的设置选项(在项目首选项中),尽管它与工作区设置没有什么不同。

回答by VigneshKumar S

It can be resolved as follows:

可以通过以下方式解决:

  1. Go to Project properties.

  2. Then 'Java Compiler' -> Check the box ('Enable project specific settings')

  3. Change the compiler compliance level to '5.0' & click ok.

  1. 转到项目属性。

  2. 然后“Java 编译器”-> 选中该框(“启用项目特定设置”)

  3. 将编译器合规性级别更改为“5.0”并单击“确定”。

Do rebuild. It will be resolved.

进行重建。它会得到解决。

Also, click the checkbox for "Use default compliance settings".

此外,单击“使用默认合规性设置”复选框。

回答by Jonnabels Zerna

Go to Project properties.

转到项目属性。

Then 'Java Compiler' -> Check the box and "Enable project specific settings"

然后“Java Compiler”-> 选中该框并“启用项目特定设置”

Uncheck the box to "Use Compliance from execution environment 'OSGi/Minimum-1.2' on the Java"

取消选中“在 Java 上使用来自执行环境‘OSGi/Minimum-1.2’的合规性”复选框

Then change the compiler compliance level to '1.5' and click Ok.

然后将编译器合规性级别更改为“1.5”并单击“确定”。

Rebuild it and your problem will be resolved.

重建它,您的问题将得到解决。

回答by insane User

I'm currently working with Eclipse Luna. And had the same problem. You might want to verify the compiler compliance settings, go to "Project/Properties/Java Compiler"

我目前正在使用 Eclipse Luna。并有同样的问题。您可能想要验证编译器合规性设置,请转到“项目/属性/Java 编译器”

The Compiler compliance level was set to 1.4, I set mine to 1.5,(and I'm working with the JDK 1.8); it worked for me.

编译器合规性级别设置为 1.4,我将我的设置为 1.5,(我正在使用 JDK 1.8);它对我有用。

And if you had to change the setting; it might be useful to go to "Window/Preferences/Java/Compiler" And check to see that the Compiler compliance level is 1.5 or higher. Just in case you have a need to do another Java project.

如果您必须更改设置;转到“Window/Preferences/Java/Compiler”并检查编译器合规性级别是否为 1.5 或更高可能会很有用。以防万一您需要做另一个 Java 项目。