在 Eclipse 中编程 Java 7

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

Programming Java 7 in Eclipse

javaeclipsejava-7

提问by reprogrammer

I installed JDK 7and Eclipse 3.6M6. Then, I added JRE 7 as a new JRE execution environment in Eclipse, and set the compiler compliance level to Java 7. I can compile the following piece of code through command line using the javacthat comes with JDK 7.

我安装了JDK 7Eclipse 3.6M6。然后,我在 Eclipse 中添加了 JRE 7 作为新的 JRE 执行环境,并将编译器合规级别设置为 Java 7。我可以使用javacJDK 7 自带的命令行编译以下代码段。

import java.util.HashMap;
import java.util.Map;

public class Try {

    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
    }
}

But, Eclipse gives the following error messages.

但是,Eclipse 会提供以下错误消息。

Incorrect number of arguments for type HashMap; it cannot be parameterized with arguments Try.java /TryJava7/src line 7 Java Problem

Syntax error on token "<", ? expected after this token Try.java /TryJava7/src line 7 Java Problem

HashMap 类型的参数数量不正确;它不能用参数参数化 Try.java /TryJava7/src 第 7 行 Java 问题

标记“<”上的语法错误,?此令牌后预期 Try.java /TryJava7/src 第 7 行 Java 问题

Even though I've set the compliance level of the compiler to Java 7, it looks like Eclipse doesn't understand Java7 syntax yet. Is it possible to play with Java 7 in Eclipse?

尽管我已将编译器的合规性级别设置为 Java 7,但 Eclipse 似乎还不了解 Java7 语法。是否可以在 Eclipse 中使用 Java 7?

The following is the content of .classpath.

以下为内容.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
  <classpathentry kind="output" path="bin"/>
</classpath>

And, the following is the content of .settings/org.eclipse.jdt.core.prefs.

并且,以下是内容.settings/org.eclipse.jdt.core.prefs

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7

采纳答案by reprogrammer

See http://wiki.eclipse.org/JDT_Core/Java7, http://wiki.eclipse.org/PDE/API_Tools/Java7and Bug 288548for the ongoing support of Eclipse for Java 7. And see http://wiki.eclipse.org/JDT/Eclipse_Java_7_Support_%28BETA%29for instructions on how to evaluate Java 7 in Eclipse.

http://wiki.eclipse.org/JDT_Core/Java7http://wiki.eclipse.org/PDE/API_Tools/Java7错误288548为Java 7中的持续支持Eclipse的,看到的http://维基.eclipse.org/JDT/Eclipse_Java_7_Support_%28BETA%29有关如何在 Eclipse 中评估 Java 7 的说明。

UPDATE 1:The BETA_JAVA7branch has been merged to HEADand R3_7_maintenance(See the eclipse-dev archive).

更新1:BETA_JAVA7分支已经合并HEADR3_7_maintenance(见Eclipse的开发归档)。

UPDATE 2:Eclipse 3.7.1 (Indigo SR1) supports Java 7.

更新 2:Eclipse 3.7.1 (Indigo SR1) 支持 Java 7

回答by Axel Gneiting

Eclipse has its own embedded Java compiler. So if there is no eclipse beta or something like that that already can compile Java 7 then you're out of luck.

Eclipse 有自己的嵌入式 Java 编译器。因此,如果没有 eclipse beta 或类似的东西已经可以编译 Java 7,那么你就不走运了。

Netbeans should work.

Netbeans 应该可以工作。

回答by Pascal Thivent

As Alex pointed out, Eclipse uses its own compiler which currently doesn't support Java 7 and, as noted in the Project Plan For Eclipse Project, version Helios, support for Java 7 is deferred and decoupled from the 3.6 release:

正如 Alex 指出的那样,Eclipse 使用自己的编译器,该编译器目前不支持 Java 7,并且如Eclipse 项目项目计划中所述,版本 Helios对 Java 7 的支持被推迟并与 3.6 版本分离:

  • ((new)deferred) Add support for Java SE 7 features. The next feature release of Java SE is version 7, which will probably be available in the second half of 2010. While the contents of this release are still under discussion, that release is expected to contain extensions to the Java language, including annotations on types (JSR-308), modularity support (JSR-294), and other minor language changes (Coin project). Eclipse Java tooling will include initial support for compiling, editing, and launching applications for Java 7 for those parts which have publicly available specifications (only JSR-308 at this point). [JDT Core, JDT UI] (288548)

    NOTE:In order to align our schedule with the delayed official Java 7 appearance and due to lack of publicly available specifications (including lack of a Java 7 JSR), we have decided to move the development work to a separate branch and decouple it from the 3.6 release. In that branch we will continue to develop the Java 7 features as they become publicly accessible. We will deliver separate updates for the official builds in order to provide early access to Java 7 features.

  • (新的)延期)添加对 Java SE 7 功能的支持。Java SE 的下一个功能版本是第 7 版,它可能会在 2010 年下半年推出。虽然此版本的内容仍在讨论中,但预计该版本将包含对 Java 语言的扩展,包括类型注释(JSR-308)、模块化支持(JSR-294)和其他小的语言变化(Coin 项目)。Eclipse Java 工具将包括对 Java 7 的编译、编辑和启动应用程序的初始支持,这些应用程序具有公开可用的规范(此时仅 JSR-308)。[JDT 核心、JDT 用户界面] ( 288548)

    注意:为了使我们的时间表与延迟的官方 Java 7 外观保持一致,并且由于缺乏公开可用的规范(包括缺乏 Java 7 JSR),我们决定将开发工作转移到一个单独的分支,并将其与3.6 发布。在该分支中,我们将继续开发 Java 7 功能,因为它们可以公开访问。我们将为官方版本提供单独的更新,以便尽早访问 Java 7 功能。

回答by reprogrammer

I checkout the so called JSR 308 branch of org.eclipse.jdt.core using the following CVS repository information.

我使用以下 CVS 存储库信息检查 org.eclipse.jdt.core 的所谓 JSR 308 分支。

Connection type: pserver
User: anonymous
Host: dev.eclipse.org
Port: Default
Repository path: /cvsroot/eclipse
Module: org.eclipse.jdt.core
Tag: JSR_308 (Branch)

Then, I followed the instructions to install the checked out JDT in eclipse. Subsequently, I looked up org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeTypeAnnotationTest.javaand /org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.javato come up with small examples exercising JSR 308 type annotations.

然后,我按照说明在 eclipse 中安装了检出的 JDT。随后,我查了一下org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeTypeAnnotationTest.java/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java想出了一些小例子来练习 JSR 308 类型注释。

At this point, the branch supports and generates the corresponding new attributes in the .class files when annotations on types are present. But, annotation processors don't seem to work.

此时,当存在类型注释时,分支支持并在 .class 文件中生成相应的新属性。但是,注释处理器似乎不起作用。

回答by Deepak Azad