• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Scanner Variable Resource Leak  RSS feed

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

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
Posts: 39054
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 62231
  • 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.
Beware the other head of science - it bites! Nibble on this message:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!