Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:
I know of System.in, but that is displaying the inputed characters in the console and is therefore not safe.

EDIT2:
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.

like image 995
SpaceTrucker Avatar asked Mar 01 '13 14:03

SpaceTrucker


People also ask

How to read password from console in Java?

The readPassword() method of Console class in Java is used to read a password or passphrase from the console with disabled echoing. Parameters: This method does not accept any parameter. Return value: This method returns a character array that contains the password or passphrase read from the console.

Does Java have a console?

The Java Console provides information about the Java version, user home directory, and any error message that occurs while running an applet or application. You can enable the Java Console for the Windows platform using the Java Control Panel or the Java icon displayed in the Windows system tray.

What is Console class what is its use in Java?

Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. The Console class was added to java.io by JDK 6. Important Points: It is used to read from and write to the console, if one exists.


2 Answers

Maybe instead of console try using dialog with JPasswordField. Here is example from http://blogger.ziesemer.com/2007/03/java-password-dialog.html.

final JPasswordField jpf = new JPasswordField();
JOptionPane jop = new JOptionPane(jpf, JOptionPane.QUESTION_MESSAGE,
        JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = jop.createDialog("Password:");
dialog.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentShown(ComponentEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                jpf.requestFocusInWindow();
            }
        });
    }
});
dialog.setVisible(true);
int result = (Integer) jop.getValue();
dialog.dispose();
char[] password = null;
if (result == JOptionPane.OK_OPTION) {
    password = jpf.getPassword();
}
if (password != null)
    System.out.println("your password: " + new String(password));
like image 110
Pshemo Avatar answered Oct 19 '22 20:10

Pshemo


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

  • You can't use System.in because the chances are that it is not connected to a console.

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

  • 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.


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.

like image 37
Stephen C Avatar answered Oct 19 '22 20:10

Stephen C