Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 7 try-with-resource syntax error

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; 
like image 629
Jalk Avatar asked Jul 24 '26 14:07

Jalk


2 Answers

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

like image 167
Deepak Azad Avatar answered Jul 27 '26 06:07

Deepak Azad


Path and SimpleFileVisitor 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.

like image 29
Arjan Tijms Avatar answered Jul 27 '26 07:07

Arjan Tijms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!