javacommand-linepasswords

Java 1.5 Command Line Password Masking


Our server is running Java 1.5 and I am having difficulty trying to mask user input from the command line. I am executing a jar file (java -jar my.jar) and am working through command line prompts via printlns. I cannot use Java Console.


Solution

  • On a Unix system? You could try running the system program stty with argument -echo before reading the program and the program stty echo afterwards.

    ProcessBuilder pb = new ProcessBuilder("stty", "-echo");
    pb.start().waitFor();
    // Read the password here
    pb = new ProcessBuilder("stty", "echo");
    pb.start().waitFor();
    

    Note that I have not tried this! Some stty programs are happy with it and some aren't (those that aren't need their stdin to come from /dev/tty to work right...)