java 7 try-with-resource语法错误
当我尝试使用新的 Java 7 try-with-resource 时,我在 Eclipse 中的 try(BufferReader.. 行上收到“令牌“(”,{预期”的语法错误”)。(下面的代码片段)这是直接复制的来自java nio教程 http://download.oracle.com/javase/tutorial/essential/io/file.html
我已将构建路径配置为最新的 java7 位置,并且我知道它的配置正确,因为我'我使用 Path 和 SimpleFileVistor 等其他 java.nio 功能没有任何问题吗? 谢谢!
try(BufferedReader reader = Files.newBufferedReader(file, charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}catch (IOException x) {
System.err.format("IOException in reading " + file.getFileName().toString()+ x);
}
return testCaseNames;
I'm getting "Syntax error on token "(", { expected" on the try(BufferReader.. line in Eclipse when I try to use the new Java 7 try-with-resource.(code snippet below) This is directly copied from the java nio tutorial http://download.oracle.com/javase/tutorial/essential/io/file.html
I have the build path configured to latest java7 location and I know it's configured properly as I'm using other java.nio features like Path and SimpleFileVistor without any problems. Any suggestions?
Thanks!
try(BufferedReader reader = Files.newBufferedReader(file, charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}catch (IOException x) {
System.err.format("IOException in reading " + file.getFileName().toString()+ x);
}
return testCaseNames;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查项目属性中的编译器合规级别,它应该是 1.7。另外您使用的是哪个版本的 Eclipse?仅在最近的版本中提供支持 - 请参阅 http://thecoderlounge.blogspot.com/2011/07/java-7-support-in-eclipse-371-38-42.html
Please check the compiler compliance level in project properties, it should say 1.7. Also which version of Eclipse are you using ? The support in present only in recent builds - see http://thecoderlounge.blogspot.com/2011/07/java-7-support-in-eclipse-371-38-42.html
Path
和SimpleFileVisitor
是库类,不需要任何特定编辑器支持新语法。在 Eclipse 中,不是 JDK 编译类,而是 Eclipse 自己的编译器
JDT
。这是应该支持新语法的版本,并且仅在 Eclipse 3.7 的最新版本中可用,而不是在可以从正常下载页面下载的任何官方版本中可用。Path
andSimpleFileVisitor
are library classes and don't require any specific editor support for the new syntax.In Eclipse it's not the JDK that's compiling the classes, but Eclipse's own compiler
JDT
. This is the one that should support the new syntax and is only available in the most recent builds of Eclipse 3.7 and not in any official version that you can download from the normal download page.