Friday October 21, 2005
java.io.Console is finally here! One of the most popular feature requests for J2SETM
in recent times has been the
request to improve console support and provide a way to enter passwords with echo
disabled. Developers know this feature
4050435 as
it has been skulking in the
Top 25 RFEs list for some time.
The good news is that the feature has made it into
Mustang thanks to Xueming Shen.
It went into b57 and should show up be on the
download site later today. The feature adds
java.io.Console which provides methods to read lines and passwords from the
console. It also provides useful methods to write formatted strings to the console too.
Here's a little taster that prompts user to enter a password that is at least
8 characters in length. The password is not echoed to the console as it
is entered.
static final int MIN_PASSWORD_LENGTH = 8;
char[] password;
do {
password = System.console().readPassword(
"Enter password (minimum of %d characters): ", MIN_PASSWORD_LENGTH);
} while (password.length < MIN_PASSWORD_LENGTH);
|
System.console() is used to obtain the unique Console for the Java virtual machine. There may not be a console of course - it depends on the platform, and also on how the Java virtual machine was started. If there isn't a console then the console() method returns null (the above code fragment doesn't check for this).
The readPassword method writes the prompt and reads the password. The prompt is provided as a format string and an argument list. If you've used the formatted printing support that was added in J2SE 5.0 then you'll recognize this.
Another thing about this code fragment is that it leaves you with a password in a character array. As with anything sensitive you don't want to have this in memory for a long time so it's good to zero the array as soon as you can.
So if you develop applications that need to access a character based console then you should find java.io.Console very useful (and very simple to use). ( Oct 21 2005, 10:17:28 AM PDT ) Permalink Comments [39]
Posted by 62.216.120.250 on October 21, 2005 at 03:22 PM PDT #
Posted by 192.18.42.10 on October 22, 2005 at 01:55 PM PDT #
Posted by Donald Guy on October 23, 2005 at 09:29 AM PDT #
62.216.120.250 - For methods that return something that isn't a boolean then the general convention nowadays is to name the method with a noun or a noun phrase. When there are methods to set and get an attribute then setAttribute/getAttribute would be usual. Also if the class is a bean the bean-standard for naming setters and getters would have to be used but this isn't the case here.
Donald - this doesn't include curses-like getch. Rather this is more like getpass(3C) - sorry.
Posted by alanb on October 23, 2005 at 01:35 PM PDT #
Posted by Keith Lea on October 24, 2005 at 08:08 AM PDT #
Posted by 129.241.16.2 on October 24, 2005 at 08:38 AM PDT #
"... the general convention nowadays..." ... Huh? Since when? Where? Did I miss a memo?
I don't know which Java code you're reading, but the method you describe is certainly not a convention in the known Java world. (You won't make Java code more compact or Ruby/.../C#-like by getting rid of set/get strings...).
If Sun wants to ditch the Beans convention, then it should say so and use it in all of it's code instead of doing a halfassed job like that (the examples given in previous comments show that this convention isn't known to everyone at Sun, like java.awt.SplashScreen.getSplashScreen(),...)
Posted by murphee on October 24, 2005 at 11:42 AM PDT #
You say that there is no getch() functionality, so I assume all input is buffered, even if we use System.console().reader().read(). That is, a Java application will not see any input until the user hits Return...
Is this correct, or can we in fact get unbuffered chars by using the reader()?
This should probably be stated explicitly in the javadoc.
I'm surprised, but not unhappy, I don't think, that there are versions of readLine() and readPassword() that take prompt arguments. I wouldn't expect anything returned by the System class to have utility methods on it.
Posted by David Flanagan on October 24, 2005 at 11:42 AM PDT #
This is a long-settled matter. I griped about this back when the java.nio API was being developed for Java 1.4. I got over it. AWT and Swing use JavaBeans style conventions. Many other APIs, especially the lower-level "system" APIs like I/O stuff do not.
Posted by David Flanagan on October 24, 2005 at 11:47 AM PDT #
Posted by SWP on October 24, 2005 at 07:28 PM PDT #
Posted by David Armstrong on October 24, 2005 at 11:52 PM PDT #
Posted by alanb on October 25, 2005 at 01:45 AM PDT #
Posted by Bob Lee on October 25, 2005 at 07:15 AM PDT #
Posted by Cedric on October 25, 2005 at 07:19 AM PDT #
This isn't a bean.
IDEs expect beans to have methods that start with get and set.
This isn't a bean.
This isn't a bean.
Thanks, Sun, for this class. It is sorely needed in my area of work.
Posted by J on October 25, 2005 at 11:58 AM PDT #
Posted by Bob Lee on October 25, 2005 at 01:09 PM PDT #
Posted by Keith Lea on October 25, 2005 at 03:55 PM PDT #
<code>A method that returns the length of something should be named length, as in class String.</code>
There is, in my view, quite an area of room for growth in the conventions. The convention rules state that they should not be followed slavishly.
The engineers that designed this class felt that it should be named console(), possibly because of its clean succinctness, possibly for other reasons. Some agree with it (like me), others don't.
However, the decision has been made.
Posted by J on October 26, 2005 at 07:26 AM PDT #
Posted by Nils on October 26, 2005 at 08:49 AM PDT #
Posted by Dissatisfied on October 26, 2005 at 10:33 AM PDT #
Posted by radu on October 27, 2005 at 03:48 AM PDT #
Posted by radu on October 27, 2005 at 03:49 AM PDT #
Posted by Christopher Brown on October 28, 2005 at 06:18 AM PDT #
Posted by Bob Lee on October 29, 2005 at 08:07 AM PDT #
Posted by Alex on October 29, 2005 at 06:59 PM PDT #
Posted by Henrik Östman on October 31, 2005 at 02:49 AM PST #
Posted by Minkoo Seo on November 02, 2005 at 10:20 AM PST #
Posted by sander on November 09, 2005 at 05:06 AM PST #
Posted by Trevor on November 13, 2005 at 11:40 AM PST #
Posted by Useless on November 25, 2005 at 04:21 AM PST #
Please?! With sugar on top?
Posted by Udo on December 05, 2005 at 10:53 AM PST #
Posted by Hue on December 15, 2005 at 07:25 PM PST #
Posted by Andreas on December 19, 2005 at 02:16 PM PST #
Posted by 192.18.42.10 on February 01, 2006 at 01:58 AM PST #
Posted by joe on April 21, 2006 at 10:41 PM PDT #
Posted by Gymso on March 07, 2007 at 08:21 AM PST #
Posted by Helen Trang on April 09, 2007 at 05:14 PM PDT #
Not Sure whether the below link would help, just check out
http://www.javalobby.org/java/forums/t84689.html
Thanks,
Posted by SME Software Solutions on December 12, 2007 at 04:25 AM PST #
"System.console().readPassword" shows error!
[Z:\pass.java:9: cannot find symbol
symbol : method console()
location: class java.lang.System
password = System.console().readPassword("Enter password (minimum of %d characters): ", MIN_PASSWORD_LENGTH);
^
1 error
Tool completed with exit code 1
]
Posted by 59.145.110.90 on February 08, 2008 at 02:40 AM PST #