Hide input on command line

后端 未结 5 1533
清酒与你
清酒与你 2020-11-27 16:08

I know that command line interfaces like Git and others are able to hide input from a user (useful for passwords). Is there a way to programmtically do this in Java? I\'m ta

5条回答
  •  生来不讨喜
    2020-11-27 16:58

    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 null 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.

提交回复
热议问题