Hide input on command line

后端 未结 5 1532
清酒与你
清酒与你 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 17:10

    There is :

    Console cons;
    char[] passwd;
    if ((cons = System.console()) != null &&
        (passwd = cons.readPassword("[%s]", "Password:")) != null) {
        ...
        java.util.Arrays.fill(passwd, ' ');
    }
    

    source

    but I don't think this works with an IDE like Eclipse because the program is run as a background process rather than a top level process with a console window.

    Another approach is to use the JPasswordField in swing with the accompanying actionPerformed method:

    public void actionPerformed(ActionEvent e) {
        ...
        char [] p = pwdField.getPassword();
    }
    

    source

提交回复
热议问题