Read password from command line
There is a method “java.io.Console.readPassword(“”)” in JDK 6 to read password from command line. Unfortunately, I am working on JDK1.4
There are some articles about creating a thread to intercept the type process. But it doesn’t work well. And I also got an article which explains the cause. FYI
I need to read passwords from the command line. Is there a way to easily read characters from the command line securely in Java? Specifically, how do you echo * to the command line?
I’ve seen that question many times. Unfortunately, there does not appear to be a way to securely accept passwords on the command line from within a Java program by simply using the supplied Java APIs. Let’s look at why that is the case.
To read items in from the command line, we use the input stream System.in. If we dig into the code a bit, we see that the default System.in input stream is really just an InputStream. Digging into my implementation of Java (IBM 1.1.7), the true implementation type of that System.in is a FileInputStream. That input stream is set to read in from the source file descriptor FileDescriptor.in. However, FileDescriptor.in is initialized by natively defined code. It is left to the system to point the FileDescriptor.in at the standard input handle.
If we look at the API definition for FileInputStream, we see that the read method is natively defined. Typed data is not taken from the command line and made available from the standard in file descriptor until the return button is pressed. Since we cannot grab a character as it is typed, there is no way to intercept the character and echo a * to the command line. Instead, we can only grab complete lines.
That leaves us with few alternatives. Unfortunately, most of the solutions require us to dive into native code or employ a GUI frontend. Briefly, here are a few solutions:
Tony Sintes is a senior consultant at ObjectWave Corporation who specializes in telecommunications. Tony has worked with Java since 1997 and is a Sun-certified Java 1.1 programmer and Java 2 developer.
FileInputStream and provide your own read() implementation.http://www.javaworld.com/javaworld/javaqa/2000-04/01-qa-0407-command.html
how to reset password on windows…
[...]Read password from command line | UKoom[...]…