• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hide java password input from Console class giving null  RSS feed

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am writing a small program to prompt user for pass, but I want the user not to see what he types, like password in linux shell



giving me nullpointerexception and when i add try catch, the programs just starts and says build successful...
Using netbeans MAC OSX

Thanks
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of the Console class. It is notorious for that. If you start a Java® app from the command line with the ja‍va instruction, that command line constitutes the Console object. If however you use the javaws instruction to start an executable .jar file (as you do if you double‑click its icon), there is no visible Console and System#console returns null, which obviously you cannot use. You usually find that IDEs use something similar to javaws which means that System.console returns null. I don't think there is a quick solution to your problem.

You can try the old‑fashioned method of using a dialogue window for your inputs. Now an opton pane usually shows “clear” text, but you can probably add a JPasswordField to it.
 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I think I will go with JDialogueBox. Other methods seem too frustrating
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried printing out the Console object and the array object; if either prints null you know where the problem is. Do that inside the if so you can see whether a null is slipping past your checks. I can't see how anything would slip past that check however.
 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you tried printing out the Console object and the array object; if either prints null you know where the problem is. Do that inside the if so you can see whether a null is slipping past your checks. I can't see how anything would slip past that check however.


I have done it using onsole class but had to do it in shell
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happened when you tried it?
 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked from console, but not from netbeans, so I am running the program from console...I made the .jar file with a shell file and entered java -jar my.jar.jar and saved it as file.sh executable so any user can run from the shell ./file.sh and it works and password does not show


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!