为什么console.readpassword在Eclipse中不起作用?

时间:2018-03-26 07:03:24

标签: java eclipse

我正在尝试从用户那里获取输入(隐藏) 并尝试打印该密码,但控制台为null并获得NullPointerException

java中是否支持readPassword()?有什么替代品?

Console cons;
if((cons = System.console()) != null) {
    char[] password = null;

    try {   
        System.out.println("Enter the password :");
        password=cons.readPassword();    
        System.out.println("Your password is" + new String(password));
    } finally {
        if(password != null) {
            java.util.Arrays.fill(password,' ');
        }
    }
} else {
    throw new RuntimeException("can't get password...No console");
}

1 个答案:

答案 0 :(得分:1)

问题是Eclipse中存在一个导致System.console为NULL的漏洞。相关问题:

How to read password from console without using System.console()?

仍然开放的错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429

另请参阅该错误的副本。