Share on Google+Share on Google+

How to read password from the console

This tutorial demonstrate how to read password without showing it on the console


Console class was introduced in jdk 1.6. This class help in taking the input from the console using its readPassword method . Here in this sample program it will ask to feed the password. Note it will accept the password without showing it.



public class ConsoleExample2 {
  public static void main(String args[]) {
    Console con;
    char[] password;
    if ((con = System.console()) != null
        && (password = con.readPassword("[%s]""Enter password:")) != null) {
      java.util.Arrays.fill(password, ' ');



Posted on: April 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.