Java Console

Using scanner read user password from terminal isn’t practical due to sensitive data. Alternatively we can use Console class.

public class Test {

 public static void main(String[] args) {
   Console console = System.console();
   if (console == null) {
      System.out.println("No console available");
   System.out.print("Please enter password");
   char[] password = console.readPassword();
   System.out.println("Password = " + String.valueOf(password));

Note! This program can be only execute from the terminal, it will not work in the IDE run java program.

Java Console

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.