Java Console

Java Console

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

import java.io.Console;
public class Test {

 public static void main(String[] args) {
   Console console = System.console();
   if (console == null) {
      System.out.println("No console available");
      return;
   }
		
   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.

Write a Reply or Comment

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