在 Eclipse 中进行 Java 7 编程
我安装了 JDK 7 和 Eclipse 3.6M6。然后,我在Eclipse中添加了JRE 7作为新的JRE执行环境,并将编译器合规级别设置为Java 7。我可以使用JDK自带的javac
通过命令行编译以下代码7.
import java.util.HashMap;
import java.util.Map;
public class Try {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
}
}
但是,Eclipse 给出以下错误消息。
HashMap 类型的参数数量不正确;它无法使用参数进行参数化 Try.java /TryJava7/src line 7 Java Problem
标记“<”存在语法错误,?在此标记后预期 Try.java /TryJava7/src line 7 Java Problem
尽管我已将编译器的合规级别设置为 Java 7,但 Eclipse 似乎尚不理解 Java7 语法。可以在 Eclipse 中使用 Java 7 吗?
以下是.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>
并且,以下是.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
I installed JDK 7 and 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 javac
that comes with JDK 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.
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
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?
The following is the content of .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
.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 Alex 指出的,Eclipse 使用自己的编译器,目前不支持 Java 7,并且如 Eclipse 项目 Helios 版本的项目计划,对 Java 7 的支持已推迟并与 3.6 版本分离:
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:
请参阅http://wiki.eclipse.org/JDT_Core/Java7、http://wiki.eclipse.org/PDE/API_Tools/Java7 和 Bug 288548 了解 Eclipse for Java 7 的持续支持。请参阅 http://wiki.eclipse.org/JDT/Eclipse_Java_7_Support_%28BETA%29 有关如何在 Eclipse 中评估 Java 7 的说明。
更新 1:
BETA_JAVA7
分支已合并到HEAD
和R3_7_maintenance
(请参阅 eclipse-dev 存档)。更新2: Eclipse 3.7.1 (Indigo SR1) 支持 Java 7 。
See http://wiki.eclipse.org/JDT_Core/Java7, http://wiki.eclipse.org/PDE/API_Tools/Java7 and Bug 288548 for the ongoing support of Eclipse for Java 7. And see http://wiki.eclipse.org/JDT/Eclipse_Java_7_Support_%28BETA%29 for instructions on how to evaluate Java 7 in Eclipse.
UPDATE 1: The
BETA_JAVA7
branch has been merged toHEAD
andR3_7_maintenance
(See the eclipse-dev archive).UPDATE 2: Eclipse 3.7.1 (Indigo SR1) supports Java 7.
Eclipse 有自己的嵌入式 Java 编译器。因此,如果没有 eclipse beta 或类似的东西已经可以编译 Java 7 那么你就不走运了。
Netbeans 应该可以工作。
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.
Netbeans should work.
我使用以下 CVS 存储库信息检查了 org.eclipse.jdt.core 的所谓 JSR 308 分支。
然后,我按照说明在 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 类型注释的小示例。此时,当存在类型注释时,分支支持并在 .class 文件中生成相应的新属性。但是,注释处理器似乎不起作用。
I checkout the so called JSR 308 branch of org.eclipse.jdt.core using the following CVS repository information.
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.java
and/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java
to come up with small examples exercising JSR 308 type annotations.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.
JDT 现已提供 Java 7 支持(BETA)。请参阅 http://thecoderlounge.blogspot。 com/2011/06/java-7-support-in-eclipse-jdt-beta.html
Java 7 support (BETA) is now available in JDT. See http://thecoderlounge.blogspot.com/2011/06/java-7-support-in-eclipse-jdt-beta.html