我正在使用System.console()
来读取用户密码。
我的代码:
Console console = System.console();
if (console == null) {
throw new RuntimeException("No console is available for input");
}
System.out.print("Enter the password: ");
String password = new String(console.readPassword());
当我直接从Redhat
运行它时,一切正常。
问题是当我尝试从文本文件中读取密码时:
java -jar my_jar.jar < password.txt
我得到:
没有可用于输入的控制台。
我应该如何解决?