I'm writing a commandline program that prompts for a passwd and I don't want it to do local echo of the password characters. After some searches, I have stumbled upon System.console().readPassword()
, which seems great, except when dealing with pipes in Unix. So, my example program (below) works fine when I invoke it as:
% java PasswdPrompt
but fails with Console == null when I invoke it as
% java PasswdPrompt | less
or
% java PasswdPrompt < inputfile
IMHO, this seems like a JVM issue, but I can't be the only one who has run into this problem so I have to imagine there are some easy solutions.
Any one?
Thanks in advance
import java.io.Console;
public class PasswdPrompt {
public static void main(String args[]) {
Console cons = System.console();
if (cons == null) {
System.err.println("Got null from System.console()!; exiting...");
System.exit(1);
}
char passwd[] = cons.readPassword("Password: ");
if (passwd == null) {
System.err.println("Got null from Console.readPassword()!; exiting...");
System.exit(1);
}
System.err.println("Successfully got passwd.");
}
}
From the Java documentation page :
The problem is most likely because using a pipe falls out of "interactive" mode and using an input file uses that as
System.in
, thus noConsole
.** UPDATE **
Here's a quick fix. Add these lines at then end of your
main
method :And invoke your application like
However, your prompted password will reside in plaintext (though hidden) file until the command terminates.
So, for some reason, when System.console() returns null, terminal echo is always off, so my problem becomes trivial. The following code works exactly as I wanted. Thanks for all the help.