2

when I am reading the code of opentsdb:

try {
    System.in.close();  // Release a FD we don't need.
} catch (Exception e) {
    log.warn("Failed to close stdin", e);
}

After searching this question on the Internet, I can't find a suitable answer. I don't understand why they write system.in.close(), and I want to know if we don't add this code block, what will happen?

Lino
  • 19,604
  • 6
  • 47
  • 65
Smallnine
  • 165
  • 7
  • 3
    Why do you feel compelled to do this? I see no good reason. – duffymo Mar 20 '19 at 15:50
  • The JVM will close `System.in` when the program terminates. – GBlodgett Mar 20 '19 at 15:51
  • They seem to just be asking why the library is doing it. They don't seem to show intent to do it themselves. – Carcigenicate Mar 20 '19 at 15:51
  • 1
    One thing to note tough. If you **do** close it, you cannot use the `InputStream` anymore. This could lead to exceptions _somewhere_ else in the application. I would not recommend doing this. – Glains Mar 20 '19 at 15:51

1 Answers1

4

Probably only if you used System.setIn() to override the standard input. One normally does not close the standard input, it's handled by JVM process shutdown.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111