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 But with the console failed == tap it or Anyone? Thanks in advance 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 ** Update ** It is a quick fix Add these lines at the end of your and invite your application like However, the password you indicated is in plain text (though hidden) file until the order is finished. 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:
% Java PasswdPrompt | As low as % 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.
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."); }}
System.in Uses in, so no console no. 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 (); }}
$ java PasswdPrompt .out.tmp; Less .out.tmp; Rm .out.tmp
Comments
Post a Comment