In this example used to get password using readPassword() of java.io.Console interface. System.console() is returning console interface to get the password by readPassword of Console interface.
Example for Getting password on Command Prompt
import java.io.Console;
import java.io.DataInputStream;
class Solution {
public static void main(String args[])throws Exception{
System.out.println("Enter the username");
String username = new DataInputStream(System.in).readLine();
System.out.println("Enter the password");
Console console = System.console();
char[] password = console.readPassword();
System.out.println("Username: "+username);
System.out.println("Password: "+String.valueOf(password));
}
}
How it works
Note
Running on eclipse, System.console() will return NullPointerException.