How to use Scanner to read silently from STDIN in Java?

rfgamaral picture rfgamaral · May 23, 2010 · Viewed 10.2k times · Source

I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps.

Answer

Eyal Schneider picture Eyal Schneider · May 23, 2010

The class java.io.Console may be useful:

System.console().readPassword();

This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns null.