Home Tutorial Java Corejava How to read password from the console

 
 

How to read password from the console
Posted on: April 10, 2010 at 12:00 AM
This tutorial demonstrate how to read password without showing it on the console

Description:

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.

Code:

import java.io.Console;

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, ' ');
    }
  }
}

Output:

Related Tags for How to read password from the console:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.