Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active January 6, 2022 07:13
Show Gist options
  • Save gkhays/2bebe536259344779518 to your computer and use it in GitHub Desktop.
Save gkhays/2bebe536259344779518 to your computer and use it in GitHub Desktop.
How to mask a password when input from the console. Contains a work-around for running within Eclipse; see Eclipse bug #122429.
package org.gkh;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
public class ConsoleUtil {
/**
* Handles console input when running outside of Eclipse.
*
* @param cons the console to use in order to receive input
* @param msg the prompt message
* @return the password input by the user
*/
public static String getPasswordMasked(Console cons, String msg)
{
char[] passwd;
while (true) {
passwd = cons.readPassword("%s", msg);
if (passwd != null) {
if (passwd.length > 0) {
return new String(passwd);
} else {
System.out.println("Invalid input\n");
}
}
}
}
/**
* Handles console input when running inside of Eclipse; See Eclipse bug
* #122429.
*
* @param msg the prompt message
* @return the password input by the user
* @throws IOException if password is zero-length
*/
public static String getPasswordWithinEclipse(String msg)
throws IOException
{
// In Eclipse IDE
System.out.print(msg);
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String password = reader.readLine();
if (password != null) {
if (password.length() <= 0) {
System.out.println("Invalid input\n");
throw new IOException("Error reading in password");
}
}
return password;
}
private Util() {}
public static void main(String[] args) {
String password = null;
String pwdMessage = "Enter the password: ";
Console cons = System.console();
if (cons == null) {
// We are in the Eclipse IDE.
try {
System.out.println("LOG: Running within Eclipse IDE...");
System.out.println("LOG: Password will not be masked");
password = Util.getPasswordWithinEclipse(pwdMessage);
System.out.println("LOG: Password entered");
} catch (IOException e) {
System.err.println("Error getting password" + e.getMessage());
System.exit(1);
}
} else {
password = Util.getPasswordMasked(cons, pwdMessage);
}
}
}
@gkhays
Copy link
Author

gkhays commented Apr 30, 2021

@basilroy1 the problem lies with the Eclipse integrated console. System.console() returns null when using it as opposed to an external terminal. The gist is just a hack to get it to run within Eclipse without throwing an NPE. Running in an external terminal does not echo the password.

λ java -cp bin poc.ConsoleUtil
Enter the password:

(prompt returns)

Short of implementing a custom console, you might resort to popping up a Swing dialog when running inside Eclipse, e.g.

    public static String getMaskedPasswordWithinEclipse(String msg) {
    	final String password;
    	final JPasswordField jpf = new JPasswordField();
    	password = JOptionPane.showConfirmDialog(null, jpf, msg,
    			JOptionPane.OK_CANCEL_OPTION,
    			JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION ?
    					new String(jpf.getPassword()) : "";
    	return password;
    }

image

References

  1. java.io.Console support in Eclipse IDE
  2. System.console() returns null
  3. Bug 122429 - System.console() (Java 6.0) returns null when running inside Eclipse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment