unix - How to handle java passwd reading when System.console() returns null? -


I am writing a command line program that prompts for a password and I should not echo this password to the local characters . After some searches, I stumbled on the system.console (). ReadPassword () , which looks good, except when handling pipes in Unix. Therefore, when my request is made then my example program works fine (below) when I call it:

But with the console failed == tap it

 % Java PasswdPrompt | As low as   

or % Java PasswdPrompt & lt; IMHO, it seems like a JVM issue, but I can not be the only one who has participated in this problem, so I think there are some easy steps.

Anyone?

Thanks in advance

  import java.io.Console; Public class PasswdPrompt {public static zero major (string agre []) {console cons = System.console (); If (cons == null) {System.err.println ("Exit System.console () !!! is exiting !!!"); System.exit (1); } Four passwords [] = cons.readPassword ("password:"); If (passwd == null) {System.err.println ("console.rodepassword got faucet !!! exiting ..."); System.exit (1); } System.err.println ("Successfully got password."); }}    

From Java page:

If the system.console returns NULL, then the console operation is not allowed, either because the OS does not support them or because the program was launched in a non-environmental environment.

The problem is most likely because the use of a pipe exits from the "interactive" mode and uses the input file which is the form of System.in Uses in, so no console no.

** Update **

It is a quick fix Add these lines at the end of your main method:

  if (args.length & gt; 0) {PrintStream out = null; Try {out = new printstream (new file optup stream (args [0])); Out.print (password); Out.flush (); } Hold (exception e) {e.printStackTrace (); } Finally {if (out! = Null) out.close (); }}   

and invite your application like

  $ java PasswdPrompt .out.tmp; Less .out.tmp; Rm .out.tmp   

However, the password you indicated is in plain text (though hidden) file until the order is finished.

Comments