Hide input on command line

Try java.io.Console.readPassword. You’ll have to be running at least Java 6 though.

   /**
    * Reads a password or passphrase from the console with echoing disabled
    *
    * @throws IOError
    *         If an I/O error occurs.
    *
    * @return  A character array containing the password or passphrase read
    *          from the console, not including any line-termination characters,
    *          or <tt>null</tt> if an end of stream has been reached.
    */
    public char[] readPassword() {
        return readPassword("");
    }

Beware though, this doesn’t work with the Eclipse console. You’ll have to run the program from a true console/shell/terminal/prompt to be able to test it.

Leave a Comment