This is the question. Console application, the user enters login and password. But when entering a password, it is necessary to “mask” each character of the password with an asterisk, for example, or, as in Linux, simply not display any characters while entering the password. I found an option using a separate thread for this. It looks something like this:
package main; import java.io.*; public class Main { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter username: "); String user = in.readLine(); Thread hide = new PasswordsHider("Enter password: "); hide.start(); String pass = in.readLine(); hide.interrupt(); } private static class PasswordsHider extends Thread { public PasswordsHider(String prompt) { super("Hiding passwords thread"); System.out.print(prompt); } @Override public void run() { while (!isInterrupted()) { System.out.print("\010"); try { Thread.sleep(1); } catch (InterruptedException e) { //e.printStackTrace(); } } } } }
but during execution, instead of “overwriting” the entered characters, a bunch of spaces are simply displayed on the screen :) I tried using “\010*” instead of “\010”, but then an asterisk is added to each space)) When "\b" again has a lot of spaces in the output. Maybe if you run it from the command line it will work, but in theory - this is how I wrote it. I heard that this is a low-priority bug in Eclipse, but not a word about the idea.
GO TO FULL VERSION