System.console() returns null System.console() returns null java java

System.console() returns null


This code snippet should do the trick:

private String readLine(String format, Object... args) throws IOException {    if (System.console() != null) {        return System.console().readLine(format, args);    }    System.out.print(String.format(format, args));    BufferedReader reader = new BufferedReader(new InputStreamReader(            System.in));    return reader.readLine();}private char[] readPassword(String format, Object... args)        throws IOException {    if (System.console() != null)        return System.console().readPassword(format, args);    return this.readLine(format, args).toCharArray();}

While testing in Eclipse, your password input will be shown in clear. At least, you will be able to test. Just don't type in your real password while testing. Keep that for production use ;).


System.console() returns null if there is no console.

You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger.