开发者

How to mask a password in command line

I want to mask a password. I used System.console() but it is not better and sometimes it retu开发者_开发知识库rns null. Is there any other way to stop echo and mask a password? I am new to java. So please can anyone explain how to do this.


The best approach would be to use Java 6's Console readPassword() method.

This site has a good article on how to do it using Java 5. http://www.devdaily.com/java/edu/pj/pj010005/. Basically they wrap System.in with an InputStreamReader and read a line.

If you are on a Unix system, you could try running the system program stty with argument -echo before reading the program and the program stty echo afterwards.

ProcessBuilder pb = new ProcessBuilder("stty", "-echo");
pb.start().waitFor();
// Read the password here
pb = new ProcessBuilder("stty", "echo");
pb.start().waitFor();

Note that I have not tried this! Some stty programs are happy with it and some aren't (those that aren't need their stdin to come from /dev/tty to work right...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜