如何从控制台读取密码而不使用System.console()? [英] How to read password from console without using System.console()?

查看:226
本文介绍了如何从控制台读取密码而不使用System.console()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击此eclipse错误,其中 System.console )不可用于Java应用程序启动。我有一个小的Java应用程序,还需要输入密码,只有从IDE启动。有没有其他方法可以从控制台安全地读取密码(意味着不在控制台上显示它)只使用JDK类?

I hit this eclipse bug, where System.console() is not available for Java application launches. I have a small Java app that also requires a password to be entered that is only started from within the IDE. Is there any other way to safely read a password (meaning not displaying it on the console) from the console with only using JDK classes?

EDIT:

我知道 System.in ,但这是在控制台中显示输入的字符,因此不是安全。


I know of System.in, but that is displaying the inputed characters in the console and is therefore not safe.

EDIT2:

我还想注意,可以在Windows下创建批处理文件, bash脚本在linux / unix下的项目。通过使用系统默认编辑器在eclipse中打开此文件,它将在一个新的控制台窗口中启动,其中 System.console()可用。这样你可以从eclipse中启动应用程序。


I also want to note that one can create a batch file under windows or a small bash script under linux/unix in the project. By openening this file in eclipse with the system default editor, it will be started in a new console window where System.console() is available. This way you can start the application from within eclipse. but the project has to be built first, so that the binaries exist.

推荐答案

如果 System.getConsole ()返回 null ,这意味着就Java而言没有可用的控制台。

If System.getConsole() returns null, that means that there is no usable console as far as Java is concerned.


  • 您不能使用 System.in

即使可以,也没有可移植的方式来关闭Java中的echoing。

Even if you could, there is no portable way to turn off echoing in Java.

您可以使用Swing(或其他任何方式)弹出窗口以要求输入密码,但如果系统是无头的,工作。

You might be able to use Swing (or whatever) to pop up a window to ask for the password, but if the system is headless that won't work.

如果您准备做非便携式,那么(在Linux / UNIX上)可以尝试打开/ dev / console或/ dev / tty。然后你可以使用termios将tty驱动程序放入noecho模式。但是你需要在本地代码中至少做一些。

If you are prepared to do non-portable things, then (on Linux / UNIX) you could try opening "/dev/console" or "/dev/tty". And then you could use termios to put the tty driver into noecho mode. But you'll need to do at least some of this in native code.

这篇关于如何从控制台读取密码而不使用System.console()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆