Java - Console readPassword() method



Description

The Java Console readPassword() method reads a password from the console with echoing disabled.

Declaration

Following is the declaration for java.io.Console.readPassword() method −

public char[] readPassword()

Parameters

NA

Return Value

This method returns a character array containing the password read from the console, not including the line termination character, or null if an end of stream has been reached.

Exception

  • IOError − If an I/O error occurs.

Important Note

System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.

Example - Usage of Console readPassword() method

The following example shows the usage of Java Console readPassword() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console console = null;
      String alpha = null;
      
      try {
         // creates a console object
         console = System.console();

         // if console is not null
         if (console != null) {
            
            // read line from the user input
            alpha = console.readLine("Name: ");
            
            // prints
            System.out.println("Name is: " + alpha);
            
            // read password into the char array
            char[] pwd = console.readPassword("Password: ");
            
            // prints
            System.out.println("Password length: "+pwd.length);
         } 
         
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();      
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Name: Master Programmer
Name is: Master Programmer
Password: 
Password length: 6

Example - Validating Password Length

The following example shows the usage of Java Console readPassword() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Prompt user for a password
      char[] passwordChars = console.readPassword("Enter a password (min 8 characters): ");
      String password = new String(passwordChars); // Convert char array to string

      // Check password length
      if (password.length() < 8) {
         console.printf("Error: Password must be at least 8 characters long.\n");
      } else {
         console.printf("Password accepted.\n");
      }

      // Clear password from memory for security
      java.util.Arrays.fill(passwordChars, ' ');
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter a password (min 8 characters):

Password accepted.

Explanation

  • Obtain the Console instance using System.console().

  • Use readPassword() to securely take user input for the password (characters are hidden on the screen).

  • Convert char[] to String for easier validation.

  • Check if the password meets the minimum length requirement (at least 8 characters).

  • Display an appropriate message based on the validation result.

  • Clear the password from memory after use for security.

Example - Confirming Password Entry

The following example shows the usage of Java Console readPassword() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;
import java.util.Arrays;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Read and confirm password
      char[] password1 = console.readPassword("Enter your password: ");
      char[] password2 = console.readPassword("Confirm your password: ");

      // Check if passwords match
      if (Arrays.equals(password1, password2)) {
         console.printf("Password confirmed successfully!\n");
      } else {
         console.printf("Passwords do not match. Please try again.\n");
      }

      // Clear password arrays for security
      Arrays.fill(password1, ' ');
      Arrays.fill(password2, ' ');
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter your password:

Confirm your password:

Password confirmed successfully!

Explanation

  • We again obtain the Console instance and check if it's available.

  • The user is asked to enter a password twice using readPassword().

  • We use Arrays.equals() to check if both passwords match.

  • If they match, a success message is displayed; otherwise, an error message is shown.

  • Security practice− We use Arrays.fill(password1, ' ') and Arrays.fill(password2, ' ') to clear password data from memory after use, preventing potential security risks.

java_io_console.htm
Advertisements