我使用BufferedReader
的readLine
从用户那里获取输入/新密码,但是想要屏蔽密码,所以我尝试使用java.io.Console
类。问题是,在Eclipse中调试应用程序时,System.console()
返回null
。我是Java和Eclipse的新手,不确定这是实现的最佳方式吗?我右键单击源文件并选择"Debug As“> "Java Application”。有什么变通方法吗?
发布于 2011-12-14 21:11:05
这是eclipse的bug #122429。
发布于 2012-06-26 08:38:42
下面的代码片段应该可以解决这个问题:
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();
}
在Eclipse中测试时,您的密码输入将以清晰的形式显示。至少,您将能够进行测试。只是不要在测试时输入你的真实密码。保留它以供生产使用;)。
发布于 2010-11-17 19:13:48
如果没有控制台,则System.console()
返回null。
您可以通过adding a layer of indirection to your code或通过在外部控制台和attaching a remote debugger中运行代码来解决此问题。
https://stackoverflow.com/questions/4203646
复制相似问题