Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Try-with-Resources(Error when running try with resources in Helios Service Release 2)
Try-with-Resources [message #1245601] Fri, 14 February 2014 02:31 Go to next message
S T is currently offline S TFriend
Messages: 1
Registered: February 2014
Junior Member
I am using Eclipse IDE for Java developers, version Helios Service Release2, Build id 20110218-0911.
The machine is Windows7 x64 machine.
JRE is build 1.7.0-b147 with support for 64 bit version.

Whenever I run try-with-resources code as show below I get errors. Basically there are 2 classes-
1. WriteObject class
2. Person class

Both are reproduced below for reference-
//WriteObject Class
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WriteObject {
    public static void main (String[] args){
        System.out.println("Writing Objects....");
        
        Person mike=new Person(543,"Bulbul");
        Person sue=new Person(123,"Chintu");
        
        System.out.println(mike);
        System.out.println(sue);
        
        try(FileOutputStream fs=new FileOutputStream("d:\\people.bin")){
            ObjectOutputStream os=new ObjectOutputStream(fs);
            os.writeObject(sue);
            os.writeObject(mike);
            os.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WriteObject.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}


The Person class is reproduced below for reference-

//Person Class
import java.io.Serializable;

public class Person implements Serializable {
    private int id;
    private String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", name=" + name + '}';
    }
    
    
}



On Compilation I get a runtime error which is-

Quote:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "(", { expected
Syntax error on token ")", ; expected
Syntax error, insert "}" to complete Block

at WriteObject.main(WriteObject.java:24)


Need help as I am not able to make out whether try-with-resources is not supported with this build version of Eclipse? Is there a plugin missing? As when I run this code on Netbeans then it works perfectly okay. Need guidance as I am interested in using eclipse IDE only.
Re: Try-with-Resources [message #1245947 is a reply to message #1245601] Fri, 14 February 2014 14:00 Go to previous messageGo to next message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3071
Registered: July 2009
Senior Member
In the project's Properties, Java Build Path section, Libraries tab, what JRE System Library is listed there? In the Java Compiler section, what is the JDK Compliance set to?
Re: Try-with-Resources [message #1246215 is a reply to message #1245601] Fri, 14 February 2014 21:13 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 3871
Registered: July 2009
Senior Member

On 2/14/14, 8:40 AM, S T wrote:
> I am using Eclipse IDE for Java developers, version Helios Service
> Release2, Build id 20110218-0911.
> The machine is Windows7 x64 machine.
> JRE is build 1.7.0-b147 with support for 64 bit version.
>
> Whenever I run try-with-resources code as show below I get errors.
> Basically there are 2 classes-
....
>
> Need help as I am not able to make out whether try-with-resources is not
> supported with this build version of Eclipse? Is there a plugin missing?
> As when I run this code on Netbeans then it works perfectly okay. Need
> guidance as I am interested in using eclipse IDE only.

It is not supported. You need at least Indigo SR1 to leverage the Java 7
syntax, and then to adjust the compiler compliance values Eric
mentioned. I suggest to move all the way up to Kepler SR1.


Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:Help with search Interfacing!
Next Topic:multiple definition error
Goto Forum:
  


Current Time: Wed Oct 21 11:23:59 GMT 2020

Powered by FUDForum. Page generated in 0.02903 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top