[Question] How to input from keyboard without displaying on screen

I would like to request user to input password. It is impossible display what user typed on the screen. I would like to mask the password with "*" or no character will display when user is typing. How to do that?Thanks

Comments

: I would like to request user to input password. It is impossible : display what user typed on the screen. I would like to mask the : password with "*" or no character will display when user is typing. : How to do that?: Thanks: This question has several answers depending on which user-interface you use:

Thanks for your suggestions. But I am using eclipse to write my program. I found that there are some problem with readPasswork and Eclipse because System.Console() will return null. It doesn't work in Eclipse, but javac is ok.

: Thanks for your suggestions. But I am using eclipse to write my : program. I found that there are some problem with readPasswork and : Eclipse because System.Console() will return null. It doesn't work : in Eclipse, but javac is ok. : : : : : I would like to request user to input password. It is impossible : : : display what user typed on the screen. I would like to mask the : : : password with "*" or no character will display when user is typing. : : : How to do that?: : : Thanks: : : : : This question has several answers depending on which user-interface : : you use:: : : : - Console: Use System.Console.readPassword() method: : Link: http://java.sun.com/javase/6/docs/api/java/io/Console.html: : : : - AWT: Use the standard TextField and set the EchoChar.: : Link: : : http://java.sun.com/javase/6/docs/api/java/awt/TextField.html#setEcho: : Char(char): : : : - Swing: Use the JPasswordField: : Link: : : http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html: : : : For other UI-packages, see the documentation of that package.: : I've tested it also. Appearantly the Eclipse's JVM is slightly different than the true JRE. A program compiled in Eclipse and run from a batch file works perfectly. Here's my testing code:[code]public class Test {

public static void main(String[] args) { System.out.println(System.console().readPassword()); }}[/code][code]java -classpath [red]h:[/red] Test[/code]My test class was on the root of my H:-drive, that's why the red part is included. You should change it to your own folder if you want to run this code yourself.Workaround for Eclipse:[code] public static char[] readPassword() { Console c = System.console(); if (c == null) { String s = System.in.readLine(); return s.toCharArray(); } else { return c.readPassword(); } }[/code]You will see the password in Eclipse itself, but if you run this code outside Eclipse no password will be shown.