This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Scanner Variable Resource Leak  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I've been making a number of scanner programs, just to get used to the idea and ive noticed the same thing in each program.

Every time I make the Scanner variable, Eclipse tells me that the variable is a resource leak (it underlines the Scanners name in yellow).


Why does this happen? and how can I fix it?
I've been looking for more information but aside from generalities, I cant seem to find anything specific (links or advice would be welcome)
(Eclipse does give 2 suggestions as to how to close the leak-but I'd rather know what im doing before I do it)


(the following is an example of one of my Scanner programs)

 
author & internet detective
Sheriff
Posts: 38564
654
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel,
I added code tags to your post to make it easier to read. You can do that yourself next time. Just highlight the code and click "code." Now, onto your question.

Eclipse is trying to protect you have leaking file handles. Scanner has a few constructors. Some take an InputStream or Readable. Which means someone can write code like new Scanner(new BufferedReader(...))

When opening a stream/reader, not closing it is usually a resource leak. Eclipse is picking up on this. In fact System.in is an InputStream. The problem here is that Eclipse is wrong. You don't close System.in. Making it ok to take Eclipse's advice to suppress the error. I recommend doing it on the most granular level of your choices. That way, you don't accidentally hide other real warnings in code you write later.
 
Marshal
Posts: 60127
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It shows the hazards of IDEs which are too helpful for their own good

Somebody else had problems about closing System.in: look here. You will also see I have my own opinion of how you use a Scanner pointing to System.in: in a utility class.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!