Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Console passwords  RSS feed

 
Ron Arbel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using console, how do you echo a star whenever a user types in a letter of the password?

With every character the user enters into the password, the characters will still be processed, but the screen will print stars in it's place. How is this done with a console application?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know a way to do this prior to JDK 6, other than calling some platform-dependent code through Runtime.exec() or JNI. However in JDK 6 it's pretty easy - just use Console's readPassword() method. The only catch is, you don't get '*' printed - you get nothing. Hopefully that's close enough though, as I don't see a good alternative.
[ November 15, 2006: Message edited by: Jim Yingst ]
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really havn't thought this through so it might not be possible.

How about combine the Console.readPassword() with an action listener
of some kind, i.e. whenever a user presses a key a '*' would written
to the console?

/Svend Rost
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that readPassword() doesn't return at all until the user hits [enter]. That's normal for console-based input, as far as I know. If you create a GUI component you can be notified of keystrokes, clicks etc. as they occur, but with console input, I don't think there's any way to do that.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing has a password window that automatically keeps the password a secrect, yet echos asterisks for user-feedback. So if you don't mind using Swing, instead of the console...

-- Kaydell
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running this whle waiting for readLine() seems to work on XP:


Edited to put in the while(running) part that I failed to transcribe from my other PC!
[ November 28, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!