views:

108

answers:

2

Hi,

Is it possible to use the Java Scanner to read from STDIN silently? I mean, without outputting any pressed chars to the terminal?

+1  A: 

You might want to give java.io.Console a look

It has a readPassword method which "Reads a password or passphrase from the console with echoing disabled".

tschaible
+2  A: 

The class java.io.Console may be useful:

System.console().readPassword();

This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns null.

Eyal Schneider
Will not work on the output window of NetBeans then?
Nazgulled
@Nazgulled: i'm afraid not (at least with Eclipse)... Opening the process' console from within the Eclipse IDE is impossible according to http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide. I'm not sure about NetBeans.
Eyal Schneider
Just tested it and it's the same with NetBeans.
Nazgulled