[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What function I can use to hide user input password in java?

Posted on 2007-10-13
14
Medium Priority
?
2,080 Views
Last Modified: 2008-01-09
Hi expert,

I want to write a simple program that will ask user password, but i don't want the actual passeord character being print on the screen.  Is there a way to do it in Java?  If so, can you plese provide some sample code.

THX
0
Comment
Question by:4eyesgirl
  • 5
  • 5
  • 4
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20071788
You can use a JPasswordField
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20071792
0
 

Author Comment

by:4eyesgirl
ID: 20071824
I am not using the textbox, so can this work for command line program?

System.in.read()?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 20071831
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20071841
In Java SE 6 there is a new class added: java.io.Console

Console cons;
char[] passwd;
if ((cons = System.console()) != null &&
    (passwd = cons.readPassword("[%s]", "Password:")) != null) {
    ...
    java.util.Arrays.fill(passwd, ' ');
}

Before Java SE 6 there was no support to read passwords from console, although you can find some tricks that can get you close, but none of them works 100%.

As CEHJ says, in swing there's a JPasswordField, but this will require you to at least display a dialog window.
0
 

Author Comment

by:4eyesgirl
ID: 20071878
Bart Cr:

Does your code work?  I just copied the abovce code and System.console() is always null, why?
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20071913
How do you launch the program. When I launch it from an IDE the System.console() will be null, launching it from command line, it works (for me).
0
 

Author Comment

by:4eyesgirl
ID: 20071946
CeJH -

Did you try the code from the http://java.sun.com/developer/technicalArticles/Security/pwordmask/ ?  I cannot get it run...it hangs at
class readPassword -
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String password = "";


Enter password: ****************************************
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20071961
That's what I meant with tricks that do not work 100%
0
 

Author Comment

by:4eyesgirl
ID: 20071963
I launch the progrma from the IDE< so I cannot launch it from eclipse?
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20071980
The IDE will replace the System.in and System.out with its own streams to capture program output. Sadly enough the input doesn't work to well.
0
 

Author Comment

by:4eyesgirl
ID: 20071990
So this track only works for command line correct?  

0
 
LVL 13

Assisted Solution

by:Bart Cremers
Bart Cremers earned 1000 total points
ID: 20071997
Yep, java.io.Console is meant for command line use.

If you want a GUI (swing/awt), you can hide input using a TextField or JPasswordField.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20073256
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question