0

The below code attempts to read the password from the system console. If user doesn't enter a password within 30 seconds, the prompt is shown again. However, I notice that the cursor is not released following the first call to console.readPassword()

I did try to do console.reader().close(); in the catch block which did not help.

Some context: I'm trying this as a workaround for the following Gradle Issue which overwrites the prompt Enter password:, and users get a feeling as if the application has hanged.

Java experts please advise.

public final class RetryPassword {

  public static char [] getPassword() {
    Console console = System.console();
    Callable<char[]> password = new Callable<char[]>() {
      @Override
      public char[] call() {
        return console.readPassword("Enter password: ");
      }
    };

    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final Future<char[]> future = executor.submit(password);
    executor.shutdown();

    try { 
      return future.get(30, TimeUnit.SECONDS); 
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }


  public static void main(String[] args) {
    char [] p = getPassword(); // first call to read password
    if (p == null) {
      p = getPassword(); // try again
    }
    System.out.println(p);
  }
}
ragesh
  • 975
  • 1
  • 9
  • 12

1 Answers1

0

This can be solved the following way http://www.javaspecialists.eu/archive/Issue153.html

Solution obtained from the following question

Community
  • 1
  • 1
ragesh
  • 975
  • 1
  • 9
  • 12