Sub topic 10
java.io.Console and Passwords
This chapter assumes basic Java classes, methods, variables, exceptions, and command-line execution. It also assumes you know that String stores text and that an array stores multiple values.
java.io.Console is a Java class for interactive terminal input. It is useful when a program needs prompts or a password that should not appear while the user types it.
Getting the Console
Call System.console() to obtain the current console:
Console console = System.console();The result can be null. Java returns null when the program has no attached console. This often happens when you run a program from an IDE, redirect input from a file, or start it from a service.
Always check for null before using the object.
import java.io.Console;
public class ConsoleCheck {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No interactive console is available.");
return;
}
String name = console.readLine("Name: ");
System.out.println("Hello, " + name);
}
}Run this program from a terminal if you want to test it interactively:
javac ConsoleCheck.java
java ConsoleCheckreadLine displays its prompt, waits for a line, and returns the text without the line ending. It returns null if the input reaches the end.
Reading a Password
Use readPassword when the input is secret:
char[] password = console.readPassword("Password: ");The typed characters are not echoed to the terminal. The terminal usually shows no characters or masking characters while the user types. Your program should never print the password or include it in an exception message.
The method returns a char[], rather than a String, because an array can be cleared after use. A String cannot be changed after it is created, so its contents may remain in memory until Java removes the object.
import java.io.Console;
import java.util.Arrays;
public class Login {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console available.");
return;
}
String user = console.readLine("User: ");
char[] password = console.readPassword("Password: ");
if (password == null) {
System.err.println("Input ended before the password was read.");
return;
}
try {
System.out.println("Received credentials for " + user);
// Pass user and password to an authentication service here.
} finally {
Arrays.fill(password, '\0');
}
}
}The finally block runs whether the code succeeds or throws an exception. Arrays.fill overwrites the password array after the program finishes using it. This does not guarantee that every copy has disappeared, but it removes the copy that your code controls.
Choosing Console for Interactive Programs
Console works well for terminal-only programs that need:
- A prompt combined with each read.
- Password input without echoing typed characters.
- Simple line-based interaction.
- Explicit detection of a missing console.
It is less suitable for programs that must work in an IDE, read redirected input, or process a large input stream. For those cases, use Scanner, BufferedReader, or another input design.
Common Errors
Do not assume that System.console() is always available. Test the null case.
Do not convert the password to a String just to compare or log it. Keep it as a char[] for as long as possible.
Do not forget to clear the array in a finally block. Do not mistake clearing the array for secure authentication. A real application should delegate password verification to a carefully designed authentication component.
Exercise
Why should a program check whether System.console() returned null, and why should it clear the array returned by readPassword?
Show answer
The console can be unavailable when input is redirected or the program runs in an IDE. Clearing the password array removes the secret from the array that the program controls after it has finished using it.