Try-with-Resources [message #1245601] |
Fri, 14 February 2014 02:31 |
S T 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 #1246215 is a reply to message #1245601] |
Fri, 14 February 2014 21:13 |
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.08148 seconds