【问题标题】:java 7 try-with-resource syntax errorjava 7 try-with-resource 语法错误
【发布时间】:2011-08-06 17:56:06
【问题描述】:

当我尝试使用新的 Java 7 try-with-resource 时,我在 Eclipse 中的 try(BufferReader.. 行中收到“令牌语法错误”(“, { 预期”)。(下面的代码 sn-p ) 这个直接抄自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; 

【问题讨论】:

    标签: eclipse java-7 try-with-resources


    【解决方案1】:

    请检查项目属性中的编译器合规级别,应该是 1.7。另外,您使用的是哪个版本的 Eclipse?目前仅在最近的版本中提供支持 - 请参阅 http://thecoderlounge.blogspot.com/2011/07/java-7-support-in-eclipse-371-38-42.html

    【讨论】:

      【解决方案2】:

      PathSimpleFileVisitor 是库类,新语法不需要任何特定的编辑器支持。

      在 Eclipse 中,编译类的不是 JDK,而是 Eclipse 自己的编译器JDT。这是应该支持新语法的语法,并且仅在 Eclipse 3.7 的最新版本中可用,而在您可以从正常下载页面下载的任何官方版本中均不可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-13
        • 2016-03-12
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        相关资源
        最近更新 更多