• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java — Closing Scanner and Resource Leak  RSS feed

 
Hans Hovan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm learning Java and working on some projects for fun. One issue that I have run in to is that when I use a Scanner object Eclipse warns me that: "Resource Leak: 'scan' is never closed."

So, I added a scan.close(); at the end of my code and that takes care of the warning.

The problem comes in because I have other classes in the same package that also use scanner objects and and Eclipse tells me to close scanner in those classes respectively. However, when I do that it seems like it closes ALL of the scanner objects and I get errors during run time.

Here is an example of what causes the error:





After scanner is closed in the scanTest class and the do loop in test2 is entered again an error occurs at the line test = scan.nextInt();

I tried moving the creation of the scanner object into the do loop just to make a new object every time as well but the error still occurs.

Not sure why this is happening or how I can make sure all my I/O objects are closed out without running into problems.

One post I came across mentioned that when System.in is closed I cannot be re-opened. If this is the case would I just need to make sure a scanner object with System.in is closed at the very end of the program and @suppress all of the other scanner warnings in other classes? Or would that still leave all those scanner objects open (bad)? Or should I make a global scanner variable and then close it at the very end of my program (but some teachers have told me using global variables is frowned upon)?

Any advice would be appreciated. Thanks for helping me learn!
 
patrick Vares
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right that if you close the scanner to System.in it is closed for good (it closes the underlying stream.

In general it isn't necessary (to my knowledge) to worry about closing System.in because it isn't really locking any resources other processes might want, but eclipse will still yell at you for it.

You could fix this by instead of creating a new Scanner every time just pass in the first Scanner and only close it at the end





That code should work and make eclipse happy.

There are other ways of working around this, but this is probably the easiest

Hope that helped!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That might be an easy solution, but in my opinion the best way to use Scanners pointing to System.in is to encapsulate them in an (uninstantiable) utility class. Then you never need to open or close them. Opening occurs when the utility class is loaded and it is only closed when the JVM terminates.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that warnings from IDEs are simply warnings. you should consider them, but you are not obliged to do anything else with them.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!